2016-06-07 80 views
0

我在編寫一個python腳本時遇到了一些問題,以審覈我公司的許多不同應用程序中的所有CSV文件,而且我幾乎用概念帶給老闆,並炫耀我可以用python做什麼,但問題是,我不明白在python中的CSV類所有這一切以及......導出爲CSV將所有字符分隔爲字段

這是一個例子,計算機信息列表如下所示:

['EB-ABORTZ,True,False,False,0', 'EB-AGONCHAROVA,True,False,False,0', 
'EB-AHART-1,True,False,False,0', 'EB-AHEIDENREICH,True,False,False,0', 
'EB-ALOCKLEAR,True,False,False,0', 'EB-AMARGULIS,True,False,False,0', 
'EB-ASKLAR,True,False,False,0', 'EB-ASKLAR-1,True,False,False,0', 
'EB-ASKLAR-3,True,False,False,0', 'EB-BCHOW-1,True,False,False,0', 
'EB-BJOHNSON,True,False,False,0', 'EB-BLYLE,True,False,False,0', 
'EB-BRUSSUM,True,False,False,0', 'EB-CCLEARY,True,False,False,0', 
'EB-...] 

,這裏是什麼在最後的代碼會產生一個例子.....

"E","B","-","A","B","O","R","T","Z",",","T","r","u","e",",","F","a","l","s","e",",","F","a","l","s","e",",","0" 
"E","B","-","A","G","O","N","C","H","A","R","O","V","A",",","T","r","u","e",",","F","a","l","s","e",",","F","a","l","s","e",",","0" 
"E","B","-","A","H","A","R","T","-","1",",","T","r","u","e",",","F","a","l","s","e",",","F","a","l","s","e",",","0" 
"E","B","-","A","H","E","I","D","E","N","R","E","I","C","H",",","T","r","u","e",",","F","a","l","s","e",",","F","a","l","s","e",",","0" 
"E","B","-","A","L","O","C","K","L","E","A","R",",","T","r","u","e",",","F","a","l","s","e",",","F","a","l","s","e",",","0" 
"E","B","-","A","M","A","R","G","U","L","I","S",",","T","r","u","e",",","F","a","l","s","e",",","F","a","l","s","e",",","0" 

下面是我用來嘗試在事後將數據導出爲CSV格式的方法的副本。

def collate_computers(computers): 
    with open('results.csv', 'w', encoding='utf8',)as outfile: 
     writer = csv.writer(outfile, quoting=csv.QUOTE_ALL, delimiter=',') 
     for c in computers: 
      writer.writerow(c) 
+1

究竟是什麼問題了嗎?我可以正確使用你的代碼。什麼是您的預期輸出? – MaThMaX

回答

0

的CSV作家功能,writerow,將列表作爲輸入並將每個元素寫入該行。當您將單個字符串傳遞給函數時,作者將該字符串解釋爲字符列表。

我假設你想寫出每一行的字符串出現。因此,而不是使用writer.writerow(c),使用writer.writerow(c.split(','))。這將字符串分割成一個用逗號分隔的列表。然後

CSV文件應該是這樣的:

EB-ABORTZ,True,False,False,0 
EB-AGONCHAROVA,True,False,False,0 
EB-AHART-1,True,False,False,0 
EB-AHEIDENREICH,True,False,False,0 
EB-ALOCKLEAR,True,False,False,0 
EB-AMARGULIS,True,False,False,0 
EB-ASKLAR,True,False,False,0 
EB-ASKLAR-1,True,False,False,0 
EB-ASKLAR-3,True,False,False,0 
EB-BCHOW-1,True,False,False,0 
EB-BJOHNSON,True,False,False,0 
EB-BLYLE,True,False,False,0 
EB-BRUSSUM,True,False,False,0 
EB-CCLEARY,True,False,False,0 
+0

這工作!我希望這不是我錯過的東西! –

0

原樣,則產生的寫入行方法逗號分隔值(.csv)

csv模塊不直接支持讀取和寫入的Unicode, 但它是8比特請使用ASCII NUL 字符清除一些問題。因此,只要避免使用NUL的編碼,如 UTF-16,就可以編寫處理 編碼和解碼的函數或類。建議使用UTF-8。 更多信息here

如果您想每行1串(即每行1個列表項)

with open('results.csv', 'w') as outfile: 
    csvWriter = csv.writer(outfile) 
    for c in computers: 
     csvWriter.writerow([c.encode('utf-8')]) 
0

的問題是在computers每一項都是一個字符串,在Python字符串字符序列,所以writer.writerow()認爲每個字符是一個單獨的字段。爲了解決這個問題,你需要先split每個字符串分解成田,其中逗號都在上面:

import csv 

def collate_computers(computers): 
    with open('results.csv', 'w', encoding='utf8') as outfile: 
     writer = csv.writer(outfile, quoting=csv.QUOTE_ALL, delimiter=',') 
     for s in computers: 
      fields = s.split(',') 
      writer.writerow(fields) 

computers = [ 
    "EB-ABORTZ,True,False,False,0", 
    "EB-AGONCHAROVA,True,False,False,0", 
    "EB-AHART-1,True,False,False,0", 
    "EB-AHEIDENREICH,True,False,False,0", 
    "EB-ALOCKLEAR,True,False,False,0", 
    "EB-AMARGULIS,True,False,False,0" 
] 

collate_computers(computers) 

results.csv

"EB-ABORTZ","True","False","False","0" 
"EB-AGONCHAROVA","True","False","False","0" 
"EB-AHART-1","True","False","False","0" 
"EB-AHEIDENREICH","True","False","False","0" 
"EB-ALOCKLEAR","True","False","False","0" 
"EB-AMARGULIS","True","False","False","0"