2016-09-01 79 views
0

將數據寫入csv文件編碼問題如何解決它

import csv 
a = [u'eaTfxfwz', u'Edward', u'O\u2019Connell', u'[email protected]', u'Santa Clara', u'CA', 'UNITED STATES', u'150 Saratoga Avenue #306', u'', u'Santa Clara, CA', u'95051', u'', u'408-835-2209', u'None', u'', '', u'2012-010', u'pjOjJfwT', u'Undefined', u'Import', u'  ', ' San Jose HQ, CA ', '', '', u'Undefined', '', u'None', '', '', u'Undefined', u'Not Considered', '', u''] 
f3 = open('test.csv', 'at') 
writer = csv.writer(f3,delimiter = ',', lineterminator='\n',quoting=csv.QUOTE_ALL) 
writer.writerow(a) 

的錯誤,我得到這個錯誤

UnicodeEncodeError: 'ascii' codec can't encode character u'\u2019' in position 1: ordinal not in range(128) 

如何修復

+0

可能的重複http://stackoverflow.com/questions/18766955/how-to-write-utf-8-in-a-csv-file。 – citaret

回答

0

可以使用utf-8編碼。

import csv 
aenc=[] 
a = [u'eaTfxfwz', u'Edward', u'O\u2019Connell', u'[email protected]', u'Santa Clara', u'CA', 'UNITED STATES', u'150 Saratoga Avenue #306', u'', u'Santa Clara, CA', u'95051', u'', u'408-835-2209', u'None', u'', '', u'2012-010', u'pjOjJfwT', u'Undefined', u'Import', u'  ', ' San Jose HQ, CA ', '', '', u'Undefined', '', u'None', '', '', u'Undefined', u'Not Considered', '', u''] 
for x in a: 
    aenc.append(x.encode('utf-8')) 
with open('test.csv', 'wb') as f3: 
    writer = csv.writer(f3,delimiter = ',', lineterminator='\n', quoting=csv.QUOTE_ALL) 
    writer.writerow(aenc) 
+0

這是否爲你工作,你累了,它不是爲我工作 –

+0

再試一次,我編輯它有用 –