提问者:小点点

将数组写入csv python(一列)


我正在尝试将数组的值写入到。python中的csv文件。但当我在excel中打开文件时,数据显示在一行中。我希望有一列,其中数组的每个成员都是一行。

数组“testLabels”的形式如下:

array(['deer', 'airplane', 'dog', ..., 'frog', 'cat', 'truck'], 
  dtype='<S10')

我用来写入csv的代码是:

import csv
resultFile = open("/filepath",'wb')
wr = csv.writer(resultFile)
wr.writerows([testLabels])

任何帮助都将不胜感激。


共3个答案

匿名用户

试试这个:

wtr = csv.writer(open ('out.csv', 'w'), delimiter=',', lineterminator='\n')
for x in arr : wtr.writerow ([x])

匿名用户

您应该更改分隔符。CSV是逗号分隔的值,但Excel知道逗号是“;”(是的,很奇怪)。因此,必须添加选项分隔符=“;”,喜欢

csv.writer(myfile, delimiter=";")

匿名用户

您需要将列表中的每一项写入CSV文件中的一行,以便将它们放在一列中。

for label in testLabels:
    wr.writerows([label])