write whitespace/tab to a text file

D

dirkheld

Hi,

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()
 
?

=?iso-8859-1?q?S=E9bastien?= Weber

Le Fri, 19 Oct 2007 07:33:29 -0700, dirkheld a écrit :
Hi,

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()
Maybe :

names = ["Sebastien", "Ana", "Elodie", "Mohamed", "Antoniavna"]
maxlen = max(len(n) for n in names)
linetowrite = ""
for n in names:
linetowrite += n.ljust(maxlen + 3, ' ')
f = open('test.txt', 'w')
f.writelines(linetowrite.strip(' ') + '\n')
f.close()
 
B

Bjoern Schliessmann

dirkheld said:
f=open('/User/home/Documents/programming/python/test.txt','w')
for x in range(len(names)):
f.write(tags[x])
f.close()

Definitely consider the Python tutorial.

Also, please provide working code examples. I don't think yours will
work ;)

names = ['John','Steve','asimov','fred','jim']
f = open('/User/home/Documents/programming/python/test.txt','w')
f.write('\t'.join(names))
f.close()

A better alternative could be the csv module.

Regards,


Björn
 
M

marc wyburn

Hi,

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. Personally I would just write data straight
into Excel using the win32 extensions. I'm sure the same can be
achieved with OO on a Linux box.
 
G

Grant Edwards

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)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top