On Sun, 13 Mar 2005, Brian van den Broek wrote:
> jrlen balane said unto the world upon 2005-03-13 19:37:
> > so for example, i am creating a text file with file.write()
> >
> > how am i going to make the file a tab-delimited file??? any
> > parameters needed???
> >
>
> >>> record1 = ['Foo', 'Bar', 'Baz']
> >>> record2 = ['Ham', 'Spam', 'Eggs']
> >>> records = [record1, record2]
> >>> lines = []
> >>> for record in records:
> ... lines.append('\t'.join(record) + '\n')
> ...
The 'csv' module might also be really handy here. For example, we can
adapt the code from:
http://www.python.org/doc/lib/node617.html
Let me write Brian's example with 'csv', just to show how it works.
######
>>> record1 = ['Foo', 'Bar', 'Baz']
>>> record2 = ['Ham', 'Spam', 'Eggs']
>>> import sys
>>> writer = csv.writer(sys.stdout)
>>> for row in [record1, record2]:
... writer.writerow(row)
...
Foo,Bar,Baz
Ham,Spam,Eggs
######
By default, the 'csv' writer uses commas as separators, but we can set the
'dialect' of a csv writer to the 'excel-tab' dialect, which uses tabs as
delimiters:
######
>>> writer = csv.writer(sys.stdout, dialect='excel-tab')
>>> for row in [record1, record2]:
... writer.writerow(row)
...
Foo Bar Baz
Ham Spam Eggs
######
I hope this helps!
_______________________________________________
Tutor maillist - [email protected]
http://mail.python.org/mailman/listinfo/tutor