I want to create a text file which contains a table, created from lists. But, I don’t want to have to do something like this:
import string
print >> textfile, string.rjust(listname[0],3), string.rjust(listname[1],3),
The following code demonstrates the idea of what I would like to do, but doesn’t work:
import string
listname = ['test1','test2','test3']
i=0
for i in range(0,3):
print >> textfile, string.rjust(listname[i],5)
I would like the output to look exactly like this:
test1 test2 test3
That’s what I’m trying to do and how it makes sense in my head, but obviously that won’t (and isn’t) working.
I’ve used the join() function to print the lists nicely, but I can’t get the spacing right for the table.
Any ideas?
If I’m understanding the problem correctly, you could just do this…
No need for the
stringmodule or integers to index into the table.I’ve printed to standard out for clarity, but you can write to a file just as easily.