UnicodeDecodeError: 'utf8'解决方法


  1. UnicodeDecodeError: 'utf8' codec can't decode byte 0xd4 in position x: invalid continuation byte
  2. 更多0
  3. pythonencode字符串
  4. 在执行下面语句时会报错

  5. o = "a test of \xe9 char" #I want this to remain a string as this is what I am receiving
  6. v = o.decode("utf-8")
  7. 错误堆栈信息如下:

  8. Traceback (most recent call last):
  9.  File "<stdin>", line 1, in <module>
  10.  File "C:\Python27\lib\encodings\utf_8.py",
  11.  line 16, in decode
  12.      return codecs.utf_8_decode(input, errors, True) UnicodeDecodeError:
  13.  'utf8' codec can't decode byte 0xe9 in position 10: invalid continuation byte
  14. 这是因为\xe9不是合法的utf-8字符,不符合utf-8的定义,对于这种情况可以这么处理:

  15. o = "a test of \xe9 char" #I want this to remain a string as this is what I am receiving
  16. v = o.decode('latin-1').encode("utf-8")
  17. print v


  18. 问题
  19. UnicodeDecodeError: 'utf-8' codec can't decode byte 0x80 in position 0: invalid start byte解决方法
  20. 解决:
  21. 这是因为\xe9不是合法的utf-8字符,不符合utf-8的定义,对于这种情况可以这么处理:
  22. li.append(line.decode('utf-8','ignore

来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/91975/viewspace-2145935/,如需转载,请注明出处,否则将追究法律责任。

转载于:http://blog.itpub.net/91975/viewspace-2145935/

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值