python讀取txt文件時報錯UnicodeDecodeError: 'gbk' codec can't decode byte 0x8e in position 8: illegal multibyte sequence


python讀取文件時報錯UnicodeDecodeError: 'gbk' codec can't decode byte 0x8e in position 8: illegal multibyte sequence,如下代碼:

 1 #coding:utf-8
 2 import shutil
 3 readDir = "F:\\爬取數據\\11.txt"
 4 writeDir = "F:\\爬取數據\\22.txt"
 5 #txtDir = "/home/fuxueping/Desktop/1"
 6 lines_seen = set()
 7 outfile=open(writeDir,"w")
 8 f = open(readDir,"r",encoding='UTF-8')
 9 for line in f:
10     print(line)
11     if line not in lines_seen:
12         line = str(line)
13         outfile.write(str(line))
14         lines_seen.add(line)
15 outfile.close()
16 print ("success")

解決方案1:

f = open(readDir,"rb")

此方案雖然沒有在報錯,但寫入的數據不是想要的格式,如下:

解決方案2:

f = open(readDir,"r",encoding='UTF-8')

一切保存正常


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM