出错文件地址: 20230101/aliyun.20230101.7.史书/113.txt
链接: https://pan.baidu.com/s/1YgIr7R-pvTdvF81qsihHxg?pwd=ctyq 提取码: ctyq
报错内容: /Users/alan/temp_test/20230101/aliyun.20230101.7.史书/113.txt gb18030 转换到utf8失败, 'gb18030' codec can't decode byte 0x80 in position 21529: illegal multibyte sequence
调试信息:
(Pdb) rl b'\xa1\xa1\xa1\xa1\xa3\xac\xca\xc7\xce\xbd\xd6\xae\xe3\xda\xa1\xa3\xa1\xb6\xcb\xb5\xce\xc4\xa1\xb7\xa3\xba\xe3\xda\xa3\xac\xc3\xc5\xb9\xd8\xd2\xb2\xa1\xa3\xa1\xb6\xca\xcd\xc3\xfb\xa1\xb7\xa3\xba\xe3\xda\xa3\xac\xe3\xda\xd2\xb2\xa3\xac\xd4\xda\xc3\xc5\xc1\xbd\xc5\xd4\xa3\xac\xd6\xd0\xd1\xeb\xe3\xda\xc8\xbb\xce\xaa\xb5\xc0\xd2\xb2\xa1\xa3\xb9\xdb\xa3\xac\xb9\xdb\xd2\xb2\xa3\xac\xd3\xda\xc9\xcf\x80-w\x80 / \x82\xb4P -\x80 W -\x9eX\xcb@---P\xcd\xf2\xf0[E` \r\n' (Pdb) rl.decode("gb18030", errors="ignore") '\u3000\u3000,是谓之阙。《说文》:阙,门关也。《释名》:阙,阙也,在门两旁,中央阙然为道也。观,观也,于上-w / 偞P - W -瀀薂---P万餥E` \r\n'
初步判断是字节数据 -\x80 W -\x9eX\xcb@—P\xcd\xf2\xf0[E 这段数据\x80 导致的问题
测试用的代码:
file_path = "errors/113_gbk.txt" line = True count = 0 with open(file_path, "rb") as f: while line: try: rl = f.readline() #line = rl.decode("gb18030", errors="ignore") count +=1 if count == 181: import pdb;pdb.set_trace() print(rl) except Exception as e: print(e)
利用PyIcu可以通过GBK编码格式解码出正确内容,但GB18030会报错
(Pdb) rl b'\xa1\xa1\xa1\xa1\xa3\xac\xca\xc7\xce\xbd\xd6\xae\xe3\xda\xa1\xa3\xa1\xb6\xcb\xb5\xce\xc4\xa1\xb7\xa3\xba\xe3\xda\xa3\xac\xc3\xc5\xb9\xd8\xd2\xb2\xa1\xa3\xa1\xb6\xca\xcd\xc3\xfb\xa1\xb7\xa3\xba\xe3\xda\xa3\xac\xe3\xda\xd2\xb2\xa3\xac\xd4\xda\xc3\xc5\xc1\xbd\xc5\xd4\xa3\xac\xd6\xd0\xd1\xeb\xe3\xda\xc8\xbb\xce\xaa\xb5\xc0\xd2\xb2\xa1\xa3\xb9\xdb\xa3\xac\xb9\xdb\xd2\xb2\xa3\xac\xd3\xda\xc9\xcf\x80-w\x80 / \x82\xb4P -\x80 W -\x9eX\xcb@---P\xcd\xf2\xf0[E` \r\n' (Pdb) utf8 = UnicodeString(rl, "GB18030") *** ValueError: 'gb18030' codec can't decode byte 0x80 in position 98: reason code 1 (the code point is illegal) (Pdb) utf8 = UnicodeString(rl, "GBK") (Pdb) utf8 <UnicodeString: '\u3000\u3000,是谓之阙。《说文》:阙,门关也。《释名》:阙,阙也,在门两旁,中央阙然为道也。观,观也,于上€-w€ / 偞P -€ W -瀀薂---P万餥E` \r\n'>