write whitespace/tab to a text file

Grant Edwards grante at visi.com
Fri Oct 19 13:11:05 EDT 2007


On 2007-10-19, marc wyburn <marc.wyburn at googlemail.com> wrote:

>> I would l like to write some data to a text file. I want to write the
>> data with whitespace or tabs in between so that I create tabular
>> columns like in a spreadsheet. How can I do this in python.
>> (btw, I'm new to python)
>>
>> names = ['John','Steve','asimov','fred','jim']
>> ## output I would like in txt file : John         Steve
>> asimov      fred     jim
>>
>> f=open('/User/home/Documents/programming/python/test.txt','w')
>>         for x in range(len(names)):
>>                 f.write(tags[x])
>>         f.close()
>
> I'm not sure exactly but you'll probably need to find out what the
> ASCII code is for a tab.

You don't need to know the ASCII code.  Just use "\t":

print "%s\t%s\t%s" % (1,"two",3)

If you fixed column spacing with spaces instead of tabs:

print "%-8s%-8s%-8s" % (1,"two",3)

-- 
Grant Edwards                   grante             Yow! HAIR TONICS, please!!
                                  at               
                               visi.com            



More information about the Python-list mailing list