解决self.buf += ''.join(self.buflist) UnicodeDecodeError: 'utf8' 问题

问题

return self.outputBuffer.getvalue()
  File "C:\Python27\lib\StringIO.py", line 276, in getvalue
    self.buf += ''.join(self.buflist)
UnicodeDecodeError: 'utf8' codec can't decode byte 0xd0 in position 7: invalid continuation byte

原因: 查看getvalue的注释

    def getvalue(self):
        """
        Retrieve the entire contents of the "file" at any time before
        the StringIO object's close() method is called.

        The StringIO object can accept either Unicode or 8-bit strings,
        but mixing the two may take some care. If both are used, 8-bit
        strings that cannot be interpreted as 7-bit ASCII (that use the
        8th bit) will cause a UnicodeError to be raised when getvalue()
        is called.
        """
        import chardet
        _complain_ifclosed(self.closed)
        if self.buflist:
            print type(self.buflist)
            print "buflist: ",self.buflist
            # print "detect:",chardet.detect(self.buflist)
            self.buf += ''.join(self.buflist)
            print "buf: ",self.buf
            self.buflist = []
        return self.buf

写的很清楚:

**The StringIO object can accept either Unicode or 8-bit strings,
but mixing the two may take some care. If both are used, 8-bit
strings that cannot be interpreted as 7-bit ASCII (that use the
8th bit) will cause a UnicodeError to be raised when getvalue()
is called.**
解释成中文:
StringIO对象可以接受Unicode或8位字符串,但混合这两者可能需要小心

解决办法:
删除以下相关语句

print "__file__:",__file__
print "__file__:", chardet.detect(__file__)

因为__file__是’GB2312’编码

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值