python读取文件时报错UnicodeDecodeError: 'gbk' codec can't decode byte 0x8e in position 8: illegal multibyte sequence,如下代码:
#coding:utf-8
import shutil
readDir = "F:\\爬取数据\\11.txt"
writeDir = "F:\\爬取数据\\22.txt"
#txtDir = "/home/fuxueping/Desktop/1"
lines_seen = set()
outfile=open(writeDir,"w")
f = open(readDir,"r",encoding='UTF-8')
for line in f:
print(line)
if line not in lines_seen:
line = str(line)
outfile.write(str(line))
lines_seen.add(line)
outfile.close()
print ("success")
解决方案1:
f = open(readDir,"rb")
此方案虽然没有在报错,但写入的数据不是想要的格式,如下:
解决方案2:
f = open(readDir,"r",encoding='UTF-8')
一切保存正常