首页 > 代码库 > python打开文件查询字符串时报UnicodeDecodeError: 'gbk' codec can't decode byte 0xaa in position 19: illegal multibyte sequence错误

python打开文件查询字符串时报UnicodeDecodeError: 'gbk' codec can't decode byte 0xaa in position 19: illegal multibyte sequence错误

 

当这样打开时报错了

lines = open(path).readlines()
open(path).close()
for line in lines:
idx1 = line.find(‘检测到的 SN 为:‘)
idx1 += len(‘检测到的 SN 为:‘)
idx2 = line.find(‘ 或许与您申请的SN不符,请联系技术提供方‘,idx1)
responsetimestr = line[idx1:idx2]
rts = responsetimestr
 

UnicodeDecodeError: ‘gbk‘ codec can‘t decode byte 0xaa in position 19: illegal multibyte sequence

 

改为以下即可

lines = open(path,mode=r,encoding=UTF-8).readlines()

 

python打开文件查询字符串时报UnicodeDecodeError: 'gbk' codec can't decode byte 0xaa in position 19: illegal multibyte sequence错误