读取文本遇到错误
‘utf-8’ codec can’t decode byte 0xa3 in position 49: invalid start byte
换用其他编码格式后错误依旧
检查后发现文本中有多种编码后的字符。
notepad可以正常显示混合编码,在notepad中转换编码格式为gbk(或其他),后可以正常读取,但是notepad没找到命令行接口。找到了可以@我。
打开文件时改为使用open(file_name,‘r’,encoding=‘gbk’,errors=‘replace’)
错误消失,运行正常。
处理字符串的混合编码时可以使用’str’.decode(‘gbk’,‘replace’)
详见ANACONDA3\lib\codecs.py codec类注释:
“”" Defines the interface for stateless encoders/decoders.
The .encode()/.decode() methods may use different error
handling schemes by providing the errors argument. These
string values are predefined:
'strict' - raise a ValueError error (or a subclass)
'ignore' - ignore the character and continue with the next
'replace' - replace with a suitable replacement character;
Python will use the official U+FFFD REPLACEMENT
CHARACTER