[英]Write multiple numpy arrays along with strings in csv
Seems like a simply thing to do, but yet I fail. 似乎只是一件简单的事情,但我失败了。 I want to write the following arrays into one line in a csv file, alongside with other strings (ie
"\\n"
). 我想将以下数组与其他字符串(即
"\\n"
)一起写入csv文件的一行中。 The values should be separated by semicolons. 值应以分号分隔。
I create the arrays from lists 我从列表创建数组
auth_list = ['5,2.6,5.23515389636e-11,0.00444005161574,0.0644299145707,0.04,0.0037037037037', '1,5.0,5.65187364062e-12,0.0,0.0605326876513,0.0,0.000740740740741']
aff_list = [(1, 0.003105590062111801), (1, 0.001838235294117647)]
comm_list = ['', '4,17.5,0.00584085856718,0.0002890919969,0.278790504782,0.0140752484561,0.0029973357016,0.00044404973357', '0,0.0,0.000218693005322,3.33471210416e-07,0.232075228649,0.0,0.000222024866785,0.0', '0,0.0,0.00025235732634,6.73003774237e-07,0.233652374653,0.0428571428571,0.000555062166963,0.0']
I proceed turning them into arrays 我继续将它们变成数组
import numpy
auth_array = numpy.genfromtxt(auth_list, delimiter=",")
aff_array = numpy.array(aff_list) # Here I don't have to use numpy.genfromtext, don't know why
auth_array = numpy.genfromtxt(aff_list, delimiter=",")
These arrays already have no commas. 这些数组已经没有逗号了。 I calculate the means using
我使用计算平均值
auth_mean = numpy.mean(auth_array, axis=0)
aff_mean = numpy.mean(aff_array, axis=0)
comm_mean = numpy.mean(comm_array, axis=0)
title = "Happy New Year"
Using print
, I see in the terminal 使用
print
,我在终端中看到
auth_mean = [ 3.00000000e+00 3.80000000e+00 2.90017063e-11 2.22002581e-03
6.24813011e-02 2.00000000e-02 2.22222222e-03]
aff_mean = [ 1. 0.00247191]
comm_mean = [ 1.33333333e+00 5.83333333e+00 2.10396963e-03 9.66994906e-05
2.48172703e-01 1.89774638e-02 1.25814091e-03 1.48016578e-04]
The arrays always have the same dimension. 数组始终具有相同的尺寸。
output_text = title + str(auth_mean).strip('[]') + ";" + str(com_mean).strip('[]') + ";" + str(aff_mean).strip('[]') + "\n"
output_file = open(output_file_name, 'w')
output_file.write(output_text)
output_file.close()
yields 产量
Happy New Year;3.00000000e+00 3.80000000e+00 2.90017063e-11 2.22002581e-03
6.24813011e-02 2.00000000e-02 2.22222222e-03; 1.33333333e+00 5.83333333e+00 2.10396963e-03 9.66994906e-05
2.48172703e-01 1.89774638e-02 1.25814091e-03 1.48016578e-04; 1. 0.00247191
How can I make a simple straight line like 我怎样才能像一条简单的直线
Happy New Year;3.00000000e+00;3.80000000e+00;2.90017063e-11;2.22002581e-03;6.24813011e-02;2.00000000e-02;2.22222222e-03;1.33333333e+00;5.83333333e+00;2.10396963e-03;9.66994906e-05;2.48172703e-01;1.89774638e-02;1.25814091e-03;1.48016578e-04;1.;0.00247191
I added commas to separate the elements of your three lists (as queried by MERose above), and I also had to initialise output_file_name: output_file_name = "output.csv"
. 我添加了逗号来分隔三个列表的元素(如上面的MERose所查询),并且我还必须初始化output_file_name:
output_file_name = "output.csv"
。
I hope this isn't a stupid question, but is it possible you are checking the outputted file in a text editor with "word wrap" turned on? 我希望这不是一个愚蠢的问题,但是有可能在打开“自动换行”的文本编辑器中检查输出的文件吗? I ran the code, and I get a single line of output as you require.
我运行了代码,并根据需要获得了一行输出。 If I then view the file in Notepad++ with "word wrap" turned on, it appears to be on multiple lines when actually it is just one line.
如果随后在打开“自动换行”的情况下在Notepad ++中查看文件,则实际上它只是一行而已却显示为多行。
FYI: the outputted file also imports nicely into LibreOffice Calc. 仅供参考:输出的文件也很好地导入了LibreOffice Calc。
First, you forgot commas in your code, I think it is: 首先,您忘记了代码中的逗号,我认为是:
title = "Happy New Year"
auth = [ 3.00000000e+00, 3.80000000e+00, 2.90017063e-11, 2.22002581e-03,
6.24813011e-02, 2.00000000e-02, 2.22222222e-03]
aff = [ 1. , 0.00247191]
comm = [ 1.33333333e+00, 5.83333333e+00, 2.10396963e-03, 9.66994906e-05,
2.48172703e-01, 1.89774638e-02, 1.25814091e-03, 1.48016578e-04]
Now about the problem: You can merge many arrays into one using itertools chain : 现在解决问题:您可以使用itertools链将许多数组合并为一个:
from itertools import chain
out = chain([title], auth, aff, comm, ['\n'])
Then you can join this row by semicolon: 然后,您可以使用分号加入此行:
str_row = ';'.join(map(str, out))
print(str_row) #or write to file, if you need
However, I strongly suggest you not reinvent the wheel and use standard csv module for writing any csv files. 但是,我强烈建议您不要重新发明轮子,而是使用标准的csv模块来编写任何csv文件。 You can specify
';'
您可以指定
';'
as delimiter when setting up csv writer. 设置csv writer时用作分隔符。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.