在python中将不同的列表导出到.txt [英] exporting different lists to .txt in python
问题描述
到目前为止,我只使用
my_array导出3个列表= numpy.array(listofrandomizedconditions)
my_array2 = numpy.array(inputsuser)
my_array3 = numpy.array(reactiontimesuser)
combined = numpy.column_stack([my_array,my_array2,my_array3])
numpy.savetxt(participantnumber +.txt,combined,delimiter =,fmt =% - 12s)
这给我一个输出像
CongruentPositief没有输入或错误的按钮没有反应时间
IncongruentNegPos没有输入或错误的按钮没有反应时间
由于这是很难阅读我想添加一个选项卡在所有不同的列表之间。
此外,我想添加一些不是前3个字符串的192个元素的列表,但是我收到一个错误,每个数组必须是相同的大小。有没有解决的办法?
特别是如果你从列表(字符串)开始,我没有看到使用 numpy
数组。
开始只需尝试打印值:
在[659]中:conditions = ['one','two','three']
在[660]中:values = [1,2,3]
在[661]中:other = ['xxxx','uuuuuuu','z']
格式
在[662]中:对于zipz中的xyz(条件,值,其他):
print(% s,%s,%s%xyz)
.....:
one,1,xxxx
two,2,uuuuuuu
three,3,z
精选与标签和固定长度:
在[663]中:对于xyz(条件,值,其他):
print(% - 12s\t%-12s\t%-12s%xyz )
.....:
一个1 xxxx
两个2 uuuuuu
三3 z
下一步是打开一个文件并写入,而不是 print
。
它是需要等长字符串的列堆栈。 savetxt
只需从参数(和列数)创建一个 fmt
字符串,并写入每个行
像我一样。
[667]:打开('temp.txt','w')为f:
.....:对于zipz(条件,值,其他)中的xyz:
.... 。:f.write('% - 12s,% - 12s,% - 12s\\\
'%xyz)
.....:
在[668]:cat temp.txt
one,1,xxxx
two,2,uuuuuuu
three,3,z
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.array(inputsuser)
my_array3=numpy.array(reactiontimesuser)
combined=numpy.column_stack([my_array,my_array2,my_array3])
numpy.savetxt(participantnumber + ".txt", combined, delimiter=" ", fmt ="%-12s")
This gives me an output like
CongruentPositief no input or wrong button no reactiontime
IncongruentNegPos no input or wrong button no reactiontime
Since this is quite hard to read I want to add a tab between all the different lists.
Also I want to add a few lists which aren't 192 elements long unlike the first 3, but then I get an error that every array has to be the same size. Is there a way around this?
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 in zip(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
这篇关于在python中将不同的列表导出到.txt的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!