1. 程式人生 > >python讀取txt檔案的錯誤 gbk' codec can't decode byte 0xbf in position 2: illegal multibyte sequence”的解決辦法

python讀取txt檔案的錯誤 gbk' codec can't decode byte 0xbf in position 2: illegal multibyte sequence”的解決辦法

眾所周知 ,python對檔案讀寫不友好

在我匯入一個新建utf-8 txt檔案依然報錯之後

最終

data11=open("D:/Downloads/盜墓筆記全集.txt").read().encoding='gbk')
file = open("D:/Downloads/盜墓筆記全集.txt", encoding='gbk')
data=file.read()

可以成功寫進data

最後分析資料頻率

import jieba.analyse
tag=jieba.analyse.extract_tags(data,15)
print(tag)

最終consel出現

Building prefix dict from the default dictionary ...
Dumping model to file cache C:\Users\Administrator\AppData\Local\Temp\jieba.cache
Loading model cost 2.041 seconds.
Prefix dict has been built succesfully.
['涓涓', '病鏈', '磋交', '涓嶇', '擄細', '嫻峰', '嶄細', '轟簡', '縐嶇', '簡寰', '勾杞', '嫻鋒', '氣滄', '戒漢', '達細']