我有一些unicode的数据,我试图写入csv文件。
我得到UnicodeEncodeError:' ascii'编解码器无法编码u' \ xa0' ...
使用以下代码
import csv
row_all = [u'\n832\xa0\nANDY\n10-04-1987\xa0\nC\n18-02-2015\xa0\nDR\xa0\n2015\xa0\nJS\xa0\nWR\xa0\nAOM/M/BCT\xa0\n\xa0\n90000000\xa0\n'],[u'\n833\xa0\nB V BOND JAMES\n07-04-1962\xa0\nSC\n21-04-1986\xa0\nP\xa0\n2015\xa0\nJAG\xa0\nSCR\xa0\nDY CCM/FS\xa0\n\xa0\n9701370958\xa0\n'],[u'\n834\xa0\nCATHY \n20-02-1983\xa0\nNR\n02-09-2013\xa0\nD\xa0\nNR\xa0\nJS\xa0\nNR\xa0\nAOM/M/MB\xa0\n\xa0\n9760534923\xa0\n'],[u'\n835\xa0\nDAN CLINTON\n31-07-1985\xa0\nCR\n17-12-2012\xa0\nDR\xa0\nNWR\xa0\nJS\xa0\nNWR\xa0\nAOM/C/AII\xa0\n\xa0\n9001196905\xa0\n']
header_row_list = [ "S_No", "NAME" , "Date_of_birth" , "Zone" , "Date_service_joined" , "Recruitment_method" , "Batch" , "Grade" , "Posted_at" , "Designation" , "Telephone_No" , "Mobile" ]
lst_dx = []
for i in row_all:
dx_row = {}
a1 = i[0].split('\n')
for j in range(1,12+1):
dx_row[header_row_list[j-1]] = a1[j]
#dx_row[header_row_list[j-1]] = a1[j].strip()
lst_dx += [ dx_row ]
g_n = 'test2.csv'
g = open( g_n , 'wb' )
with g as out_file:
writer = csv.DictWriter(out_file , fieldnames = header_row_list )
writer.writeheader()
for row in lst_dx :
#writer.writerow([unicode(entry).encode('utf-8') for entry in row])
writer.writerow(row)
g.close()
我可以使用
修复此错误#dx_row[header_row_list[j-1]] = a1[j].strip()
但是还有更好的" pythonic"处理这个并改进此代码的方法?
答案 0 :(得分:0)
Python的open()
采用可选的关键字参数“encoding”。如果你把它设置为UTF-8,它会做你想要的吗?