UnicodeDecodeError: ‘gbk’ codec can’t decode byte 0x80 in position 25: illegal multibyte sequence
原代码:
fp = open('data.txt','r')
lines = []
for line in fp:line = line.strip()lines.append(line)
fp.close()lines.insert(4, '波波')
s = '\n'.join(lines)
fp = open('data.txt', 'w')
fp.write(s)
fp.close()
修改后代码:
fp = open('data.txt','r',encoding='UTF-8')
lines = []
for line in fp:line = line.strip()lines.append(line)
fp.close()lines.insert(4, '波波')
s = '\n'.join(lines)
fp = open('data.txt', 'w')
fp.write(s)
fp.close()
程序运行效果: