Python中使用codecs解決生僻字處理異常('gbk' codec can't encode...)

'gbk' codec can't encode character 'ue863'

python處理文本的時候時常會遇到生僻字出現的處理異常,查了不少資料,發現codecs能夠解決這個問題,這裏列舉一個從excel中讀取數據並寫入csv中的實例:python

#python3.4
import xlrd
import csv
import codecs
data=xlrd.open_workbook("導入.xls")
table=data.sheets()[0]
nrows=table.nrows
ncols=table.ncols
a=list()
for i in range(nrows ):
      a.append(table.row_values(i))
      a[i].append('hello')

with codecs.open('my.csv','w+','utf-8') as csv_file:  
    writer = csv.writer(csv_file)  
    for row in a:  
        writer.writerow(row)

以上代碼能夠將Excel中的生僻字寫入csv。app

相關文章
相關標籤/搜索