- UnicodeDecodeError: 'utf8' codec can't decode byte 0xd4 in position x: invalid continuation byte
- 更多0
- pythonencode字符串
- 在执行下面语句时会报错
-
- o = "a test of \xe9 char" #I want this to remain a string as this is what I am receiving
- v = o.decode("utf-8")
- 错误堆栈信息如下:
-
- Traceback (most recent call last):
- File "<stdin>", line 1, in <module>
- File "C:\Python27\lib\encodings\utf_8.py",
- line 16, in decode
- return codecs.utf_8_decode(input, errors, True) UnicodeDecodeError:
- 'utf8' codec can't decode byte 0xe9 in position 10: invalid continuation byte
- 这是因为\xe9不是合法的utf-8字符,不符合utf-8的定义,对于这种情况可以这么处理:
-
- o = "a test of \xe9 char" #I want this to remain a string as this is what I am receiving
- v = o.decode('latin-1').encode("utf-8")
- print v
-
-
- 问题
- UnicodeDecodeError: 'utf-8' codec can't decode byte 0x80 in position 0: invalid start byte解决方法
- 解决:
- 这是因为\xe9不是合法的utf-8字符,不符合utf-8的定义,对于这种情况可以这么处理:
- li.append(line.decode('utf-8','ignore
来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/91975/viewspace-2145935/,如需转载,请注明出处,否则将追究法律责任。
转载于:http://blog.itpub.net/91975/viewspace-2145935/