In [458]: type(obj_xml)
Out[458]: builtins.bytes
In [459]: with codecs.open( xmlOutFile, "+ab", "utf-8" ) as f:
.....: f.write(obj_xml)
.....:
错误我正在点击
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-459-61a3d9d572a6> in <module>()
1 with codecs.open( xmlOutFile, "+ab", "utf-8" ) as f:
----> 2 f.write(obj_xml)
3
C:\Python3\lib\codecs.py in write(self, data)
698 def write(self, data):
699
--> 700 return self.writer.write(data)
701
702 def writelines(self, list):
C:\Python3\lib\codecs.py in write(self, object)
354 """ Writes the object's contents encoded to self.stream.
355 """
--> 356 data, consumed = self.encode(object, self.errors)
357 self.stream.write(data)
358
TypeError:无法隐式地将'bytes'对象转换为str 我如何将obj_xml的内容写入文件?
答案 0 :(得分:0)
codecs.open
接受Unicode字符串并在写入时将其编码为字节。你已经有了一个bytes对象,所以只需要以二进制模式打开文件文件并编写对象:
with open(xmlOutFile,'+ab') as f:
f.write(obj_xml)