1. 程式人生 > >UnicodeEncodeError: 'gbk' codec can't encode character

UnicodeEncodeError: 'gbk' codec can't encode character

在window下學習編寫python指令碼時,遇到了UnicodeEncodeError: ‘gbk’ codec can’t encode character的問題。

with open('duanzi.txt','w') as f:
    for cont in content:
        f.write(cont)

出現如下錯誤:

Traceback (most recent call last):
  File "duanzi.py", line 16, in <module>
    f.write(cont)
UnicodeEncodeError: 'gbk' codec can't encode character '\xa0' in position 8: illegal multibyte sequence

經分析,這個問題的原因是open(‘duanzi.txt’,‘w’)由於window下在建立的duanzi.txt的編碼方式為gbk,而我們爬取的內容編碼格式是utf-8。
問題解決方法:
在建立這個檔案時,設定其編碼方式為utf-8,即可通過。

with open('duanzi.txt','w',encoding='utf-8') as f:
    for cont in content:
        f.write(cont)