Exporting Different Lists To .txt In Python
I have a few lists which I all want to export to the same .txt file. So far I only export 3 of the lists using my_array=numpy.array(listofrandomizedconditions) my_array2=numpy.ar
Solution 1:
Especially if you are starting with lists (of strings) I don't see the point to using numpy
arrays.
For a start just try printing values:
In [659]: conditions=['one','two','three']
In [660]: values=[1,2,3]
In [661]: other=['xxxx','uuuuuuu','z']
basic format
In [662]: for xyz in zip(conditions, values,other):
print("%s,%s,%s"%xyz)
.....:
one,1,xxxx
two,2,uuuuuuu
three,3,z
refined with tab and fixed lengths:
In [663]: for xyz inzip(conditions, values,other):
print("%-12s\t%-12s\t%-12s"%xyz)
.....:
one 1 xxxx
two 2 uuuuuuu
three 3 z
Next step is to open a file and write to that, instead of print
.
It's column stack that requires equal length strings. savetxt
just creates a fmt
string from your parameter (and the number of columns), and writes each row
like I do.
In [667]: with open('temp.txt','w') as f:
.....: for xyz in zip(conditions,values,other):
.....: f.write('%-12s,%-12s,%-12s\n'%xyz)
.....:
In [668]: cat temp.txt
one ,1 ,xxxx
two ,2 ,uuuuuuu
three ,3 ,z
Post a Comment for "Exporting Different Lists To .txt In Python"