【問題描述】
運行環境, Ubuntu14.04 64bit
在用Python跑程序分析文本的時候出現了下面的報錯信息:
UnicodeDecodeError: 'ascii' codec can't decode byte 0xe8 in position 7:
ordinal not in range(128)
【解決方案】
具體的解決方案如下所示:
針對某一行的話我們可以這麼做:
比如line 是一個字符串變量
line = line.decode("utf-8").encode("utf-8")
or
line = line.decode("utf-8",'ignore').encode("utf-8","ignore")
針對某個文本的話,可以用下面這個方法二:
import sys
reload(sys)
sys.setdefaultencoding("utf-8")
建議使用方法二