python中遇到这个错误
UnicodeDecodeError: 'gbk' codec can't decode bytes in position 12-13: illegal multibyte sequence
错误信息:
UnicodeDecodeError: ‘XXX' codec can't decode bytes in position 2-5: illegal multibyte sequence
这是因为遇到了非法字符,例如:全角空格往往有多种不同的实现方式,比如\xa3\xa0,或者\xa4\x57,#将获取的字符串strTxt做decode时,指明ignore,会忽略非法字符,
#当然对于gbk等编码,处理同样问题的方法是类似的
strTest = strTxt.decode('utf-8', 'ignore')
return strTest
[补充]
默认的参数就是strict,代表遇到非法字符时抛出异常;