我有以下代码:
file = io.open(tFile,'wb')
if cData != '':
file.write('%s\n' % (cData))其中cData是一个字符串。该代码在Linux和Windows上运行良好,但在我所拥有的所有Solaris计算机上都失败,错误如下:
文件"/usr/local/lib/python2.6/io.py",第1045行,在写提升TypeError中(“不能将unicode写到二进制流”) TypeError:不能将unicode写到二进制流
这里怎么了?谢谢
发布于 2013-11-14 06:29:11
io.open用于与Unicode数据交互。如果您想要编写字节字符串(即Python2 str),请使用内置的open函数。
如果您真的想使用io.open,您需要对字符串进行解码(如果它是可解码的)
file.write(('%s\n' % (cData)).decode())或者使用Unicode文字。
file.write(u'%s\n' % (cData))我假设您在这里理解基本的字符串编码原则。如果cData与ASCII不兼容,您需要自己提供适当的编码名称。
https://stackoverflow.com/questions/19970927
复制相似问题