问题
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’编码