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


The idiomatic way to do this would be something like

f = open('/path/to/file.txt', 'w')
f.write('\t'.join(names))
f.write('\n')
f.close()

the .join() method on a string joins the contents of a list with
the string on which it's called (in this case, "\t" which is a tab).

-tkc



 
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
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57