views:

399

answers:

6

My Python module has a list that contains all the data I want to save as a .txt file somewhere. The list contains several tuples like so:

list = [ ('one', 'two', 'three'), ('four', 'five', 'six')]

How do I print the list so each tuple item is separated by a tab and each tuple is separated by a newline?

Thanks

+8  A: 
print '\n'.join('\t'.join(x) for x in L)
Ignacio Vazquez-Abrams
Says 'sequence item 0: expected string, tuple found'
Nimbuz
Yup. Fixed that.
Ignacio Vazquez-Abrams
+2  A: 

Try this

"\n".join(map("\t".join,l))

Test

>>> l = [ ('one', 'two', 'three'), ('four', 'five', 'six')]
>>> print "\n".join(map("\t".join,l))
one     two     three
four    five    six
>>>
S.Mark
`map` builds lists -- unnecessary.
John Machin
+2  A: 
open("data.txt", "w").write("\n".join(("\t".join(item)) for item in list))
Matt Joiner
says 'argument 1 must be string or read-only character buffer, not generator'
Nimbuz
i corrected it, try again
Matt Joiner
+1  A: 

The most idiomatic way, IMHO, is to use a list comprehension and a join:

print '\n'.join('\t'.join(i) for i in l)
tim.tadh
I see no list comprehension here.
John Machin
+6  A: 

You can solve it, as other answers suggest by just joining lines but better way would be to just use python csv module, so that later on you can easily change delimter or add header etc and read it back too, looks like you want tab delimited file

import sys
import csv

csv_writer = csv.writer(sys.stdout, delimiter='\t')
rows = [ ('one', 'two', 'three'), ('four', 'five', 'six')]
csv_writer.writerows(rows)

output:

one two three
four    five    six
Anurag Uniyal
Thanks for the alternate method but I'm too beginner to try it :)
Nimbuz
but in way it is much simpler, because you just use std libs, but yes for a beginner you must also know how to do it hardway :)
Anurag Uniyal
Good solution. Don't use 'list' for a variable name, though.
Mark Tolonen
@Mark Tolonen, thanks fixed.
Anurag Uniyal
-1 Now explain to the beginner how to do it without StringIO, which introduces unwanted complexity and is slow, and with output to a file. 5 upvoters???
John Machin
@John Machin, are we writing a beginners tutorial or correct solution, writing csv is not as trivial as it sounds and it is better to use a standard lib if available, and StringIO is used in many places in python where interface takes file type objects
Anurag Uniyal
@Anurag; I don't understand your question. csv is fine; I've got no grip with that at all. Mr gripe is with StringIO. The OP wanted to "save as .txt file" then wanted to "print the list" -- both of these can be done very simply (the latter by `import sys; csv_writer = csv.writer(sys.stdout, delimiter='\t')`) with standard file objects without the baroque StringIO.
John Machin
@John Machin, ok got it, yes StringIO wasn't needed here, removed.
Anurag Uniyal
A: 

You don't have to join the list in advance:

with open("output.txt", "w") as fp:
    fp.writelines('%s\n' % '\t'.join(items) for items in a_list)
ΤΖΩΤΖΙΟΥ