UnicodeDecodeError: ‘utf-8‘ codec can‘t decode byte 0xb4...
UnicodeDecodeError: ‘utf-8’ codec can’t decode byte 0xb4 in position 10: invalid start byte
原因:中文字符的Unicode编码0x0800-0xFFFF之间,(utf-8包含了部分汉字)
当你试图将该“中文字符”转成U码的utf-8时超出了其范筹.而GBK 规范收录了 ISO 10646.1 中的全部 CJK 汉字和符号,并有所补充
解决方法:将.decode(‘utf-8’)改为.decode(‘gbk’)
#fp = open('data.txt','r+',encoding='UTF-8')
fp = open('data.txt','r+',encoding='gbk')
lines = []
for line in fp:
line = line.strip()
lines.append(line)
fp.close()
lines.insert(6, '渣渣猫')
lines = str(lines)
s = "\n".join(lines)
fp = open('data.txt', 'w')
fp.write(s)
fp.close()