代码如下:
file_in="C:\\Users\\Administrator\\Desktop\\parking datum.txt"
data=''
fbegin=0
fend=0
d=''
with open(file_in,"r") as file:
for i in file.readlines():
for e in i:
if e=='"':
continue;
if e=='}':
fbegin=0
data=(data+d)
d=''
if fbegin==1:``
d=d+e;
if e=='{':
fbegin=1
print('----------------')
print(data)
上述代码是为了把一个文件读入为字符串并在读的过程中进行了一定的处理,但是python3.6显示:
UnicodeDecodeError: ‘gbk’ codec can’t decode byte 0x82 in position 66: illegal multibyte sequence
经过上网查询,发现txt文本分为4中编码方式:ANSI、Unicode、Unicode big endian和UTF-8。打开txt点击文件-另存为会显示当前txt的编码方式(会将当前的编码方式当成另存为的默认格式)。而该报错是指gbk编码译码器