Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > write whitespace/tab to a text file

Reply
Thread Tools

write whitespace/tab to a text file

 
 
dirkheld
Guest
Posts: n/a
 
      10-19-2007
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()

 
Reply With Quote
 
 
 
 
=?iso-8859-1?q?S=E9bastien?= Weber
Guest
Posts: n/a
 
      10-19-2007
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()
 
Reply With Quote
 
 
 
 
Bjoern Schliessmann
Guest
Posts: n/a
 
      10-19-2007
dirkheld wrote:

> 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


--
BOFH excuse #388:

Bad user karma.

 
Reply With Quote
 
marc wyburn
Guest
Posts: n/a
 
      10-19-2007
On Oct 19, 3:33 pm, dirkheld <(E-Mail Removed)> wrote:
> 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.

 
Reply With Quote
 
Grant Edwards
Guest
Posts: n/a
 
      10-19-2007
On 2007-10-19, marc wyburn <(E-Mail Removed)> 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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
When using System.IO.FileStream, I write 8 bytes, then seek to the start of the file, does the 8 bytes get flushed on seek and the buffer become a readbuffer at that point instead of being a write buffer? DR ASP .Net 2 07-29-2008 09:50 AM
When using System.IO.FileStream, I write 8 bytes, then seek to the start of the file, does the 8 bytes get flushed on seek and the buffer become a readbuffer at that point instead of being a write buffer? DR ASP .Net Building Controls 0 07-29-2008 01:37 AM
is better to open, write, close file than open, write, append, close? Iulian Ilea Javascript 1 12-21-2006 04:32 PM
python+windows/linux -> write stdout text to always on top text? flamesrock Python 5 05-04-2005 06:34 PM
Change text color for one document.write but not color of all text? kroger@princeton.edu Javascript 7 02-02-2005 01:23 PM



Advertisments