Anthony
Anthony

Reputation: 1886

print python list with width specifiers

I'm hoping there's a better way to do this. Straight to the code:

print "-I- %-6s%-6s%-6s%-6s%-6s%-6s%-6s%-6s%-8s" % \
      ("A",B","C","D","E","F","G","H","% Done")
print "-I- %-6s%-6s%-6s%-6s%-6s%-6s%-6s%-6s%-8s" % \
      ("-"*5 ,"-"*5 ,"-"*5 ,"-"*5 ,"-"*5 ,"-"*5 ,"-"*5,"-"*5,"-"*8)

Ideally, I'd want to do something like this:

hdrs = ["A",B","C","D","E","F","G","H","% Done"]
<print statement that uses len(hdrs[i]+2) for the column width>
<print statement that uses len(hdrs[i]+2) for the column width and len(hdrs[i]+1 for the number of dashes>

The output would look like this:

A     B     C
----- ----- -----

This method would be much more extensible than my current method. I've tried various things using join and map, but I haven't been able to figure out a viable solution. Any help would be greatly appreciated.

Edit:

I just got this part to work:

print " ".join("-"*(len(x)+1) for x in hdrs)

The previous line of code prints the dashes the way I requested in the original post, but I'm wondering if there's a cleaner way. I still can't figure out how to print the strings.

Upvotes: 1

Views: 2823

Answers (4)

pradyunsg
pradyunsg

Reputation: 19486

Try this, here you can specify the width for every column (Don't let the string be bigger than the width)

widths = [6,6,6,6,6,6,6,6,8]
hdrs = ["A","B","C","D","E","F","G","H","% Done"]

data = [hdrs[i].ljust(widths[i]) for i in range(len(hdrs))]
widths = ['-'*i for i in widths]

print '%s '*len(data) % tuple(data)
print ' '.join(widths)

Output

A      B      C      D      E      F      G      H      % Done 
------ ------ ------ ------ ------ ------ ------ ------ --------

Upvotes: 1

piokuc
piokuc

Reputation: 26204

You can construct your format string like this:

format = "".join(["%-"+str(len(h)+2)+"s" for h in hdrs])

and then use it to print your lists, for example:

l = range(hdrs) # example data to print, the number of items is the same as hdrs
print format % tuple(l)

Upvotes: 2

Burhan Khalid
Burhan Khalid

Reputation: 174728

If you just want to get it done and aren't doing this as an exercise, use prettytable. This example is from the linked tutorial:

x = PrettyTable(["City name", "Area", "Population", "Annual Rainfall"])
x.align["City name"] = "l" # Left align city names
x.padding_width = 1 # One space between column edges and contents (default)
x.add_row(["Adelaide",1295, 1158259, 600.5])
x.add_row(["Brisbane",5905, 1857594, 1146.4])
x.add_row(["Darwin", 112, 120900, 1714.7])
x.add_row(["Hobart", 1357, 205556, 619.5])
x.add_row(["Sydney", 2058, 4336374, 1214.8])
x.add_row(["Melbourne", 1566, 3806092, 646.9])
x.add_row(["Perth", 5386, 1554769, 869.4])
print x

Output:

+-----------+------+------------+-----------------+
| City name | Area | Population | Annual Rainfall |
+-----------+------+------------+-----------------+
| Adelaide  | 1295 |  1158259   |      600.5      |
| Brisbane  | 5905 |  1857594   |      1146.4     |
| Darwin    | 112  |   120900   |      1714.7     |
| Hobart    | 1357 |   205556   |      619.5      |
| Sydney    | 2058 |  4336374   |      1214.8     |
| Melbourne | 1566 |  3806092   |      646.9      |
| Perth     | 5386 |  1554769   |      869.4      |
+-----------+------+------------+-----------------+

Upvotes: 5

viraptor
viraptor

Reputation: 34205

How about this:

hdrs = ("A","B","C","D","E","F","G","H","% Done")
fmt_string = ''.join("%%-%is" % (len(h)+2) for h in hdrs)
print(fmt_string % hdrs)
print(fmt_string % tuple("-"*(len(h)+1) for h in hdrs))

I used the described column sizes rather than the ones from examples.

Upvotes: 3

Related Questions