Printing list into fixed width strings in Python -
with next list"
in [3]: entr = [ 'atom', '1', 'p', 'c', 'b', '3', '42.564', '-34.232', '-7.330', '1.00', '105.08', 'p' ]
i'd create string fixed width using %. why failed? both lines contain 12 entries.
in [4]: buf = "%-6s%5d %-4s%3s %1s%4d %8.3f%8.3f%8.3f%6.2f%6.2f %2s\n" % entr --------------------------------------------------------------------------- typeerror traceback (most recent phone call last) <ipython-input-4-6c71c289664e> in <module>() ----> 1 buf = "%-6s%5d %-4s%3s %1s%4d %8.3f%8.3f%8.3f%6.2f%6.2f %2s\n" % entr typeerror: not plenty arguments format string
first should give tuple
format operator %
"..." % tuple(entr)
second, should utilize %d
numbers, not strings! so, need cast variables number
. here short example;
"...%s ... %d ... %f " %(entr[1], float(entr[2]), float(entr))
python string
No comments:
Post a Comment