原代码:
a=open(r"C:\Users\*****\Desktop\one.txt","r")
b=a.read()
print(b)
报错
UnicodeDecodeError Traceback (most recent call last)
in
1 a=open(r"C:\Users****\Desktop\one.txt",“r”)
----> 2 b=a.read()
3 print(b)
UnicodeDecodeError: ‘gbk’ codec can’t decode byte 0x80 in position 21: illegal multibyte sequence
修改:
方案一:按二进制读取文件
a=open(r"C:\Users\*****\Desktop\one.txt","rb")
b=a.read()
print(b)
方案二:设置编码(推荐)
a=open(r"C:\Users\*****\Desktop\one.txt","r",encoding='UTF-8')
b=a.read()
print(b)