2014-01-30 73 views
1

我想寫一個數組的值到Python中的.csv文件。但是,當我在Excel中打開文件時,數據顯示在一行中。我想有一列,每個成員的數組是一行。寫入數組到csv python(一列)

陣列 「testLabels」 的形式爲:

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

我使用寫入CSV的代碼是:

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

任何幫助,將不勝感激。

+0

問題長期以來一直回答關於蟒蛇方的建議。但是,在MS Excel中使用函數會更容易:只需使用[text-to-columns函數](http://www.excel-easy.com/examples/text-to-columns.html)。 – MERose

回答

2

您應該更改分隔符。 CSV是逗號分隔值,但Excel瞭解逗號是「;」 (是的很奇怪)。所以,你必須添加選項分隔符=「;」像

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

分隔符excel使用可配置...但它不通過excel,但:http://www.howtogeek.com/howto/21456/export-or-save-excel-files-with-pipe-or-other-delimiters -instead-的-逗號/ – deostroll

1

您需要列出的每個項目寫入行的CSV文件,以讓他們進入一列。

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

試試這個:

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