python 数组写入文件_将数组写入csv python(一列)

1586010002-jmsa.png

I'm trying to write the values of an array to a .csv file in python. But when I open the file in excel, the data is shown in one row. I want to have one column where each member of the array is a row.

The array "testLabels" is of the form:

array(['deer', 'airplane', 'dog', ..., 'frog', 'cat', 'truck'],

dtype='

And the code I use to write to the csv is:

import csv

resultFile = open("/filepath",'wb')

wr = csv.writer(resultFile)

wr.writerows([testLabels])

Any help would be greatly appreciated.

解决方案

Try this:

wtr = csv.writer(open ('out.csv', 'w'), delimiter=',', lineterminator='\n')

for x in arr : wtr.writerow ([x])

你可能感兴趣的:(python,数组写入文件)