Python中使用codecs解決生僻

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

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

#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。

Tags: