Python builtins.TypeError:必须是str,而不是bytes

Python builtins.TypeError:必须是str,而不是bytes,python,python-3.x,lxml,Python,Python 3.x,Lxml,我已经将我的脚本从Python2.7转换为3.2,我有一个bug # -*- coding: utf-8 -*- import time from datetime import date from lxml import etree from collections import OrderedDict # Create the root element page = etree.Element('results') # Make a new document tree doc = etr

我已经将我的脚本从Python2.7转换为3.2,我有一个bug

# -*- coding: utf-8 -*-
import time
from datetime import date
from lxml import etree
from collections import OrderedDict

# Create the root element
page = etree.Element('results')

# Make a new document tree
doc = etree.ElementTree(page)

# Add the subelements
pageElement = etree.SubElement(page, 'Country',Tim = 'Now', 
                                      name='Germany', AnotherParameter = 'Bye',
                                      Code='DE',
                                      Storage='Basic')
pageElement = etree.SubElement(page, 'City', 
                                      name='Germany',
                                      Code='PZ',
                                      Storage='Basic',AnotherParameter = 'Hello')
# For multiple multiple attributes, use as shown above

# Save to XML file
outFile = open('output.xml', 'w')
doc.write(outFile) 
在最后一行中,我遇到了以下错误:

builtins.TypeError: must be str, not bytes
File "C:\PythonExamples\XmlReportGeneratorExample.py", line 29, in <module>
  doc.write(outFile)
File "c:\Python32\Lib\site-packages\lxml\etree.pyd", line 1853, in lxml.etree._ElementTree.write (src/lxml/lxml.etree.c:44355)
File "c:\Python32\Lib\site-packages\lxml\etree.pyd", line 478, in lxml.etree._tofilelike (src/lxml/lxml.etree.c:90649)
File "c:\Python32\Lib\site-packages\lxml\etree.pyd", line 282, in lxml.etree._ExceptionContext._raise_if_stored (src/lxml/lxml.etree.c:7972)
File "c:\Python32\Lib\site-packages\lxml\etree.pyd", line 378, in lxml.etree._FilelikeWriter.write (src/lxml/lxml.etree.c:89527)
builtins.TypeError:必须是str,而不是bytes
文件“C:\pythonnypes\XmlReportGeneratorExample.py”,第29行,在
文件写入(输出文件)
文件“c:\Python32\Lib\site packages\lxml\etree.pyd”,第1853行,在lxml.etree.\u ElementTree.write(src/lxml/lxml.etree.c:44355)中
文件“c:\Python32\Lib\site packages\lxml\etree.pyd”,第478行,类似lxml.etree.\u-tofilelike(src/lxml/lxml.etree.c:90649)
文件“c:\Python32\Lib\site packages\lxml\etree.pyd”,第282行,位于lxml.etree.\u ExceptionContext.\u如果存储了,则引发(src/lxml/lxml.etree.c:7972)
文件“c:\Python32\Lib\site packages\lxml\etree.pyd”,第378行,位于lxml.etree.\u FilelikeWriter.write(src/lxml/lxml.etree.c:89527)
我已经安装了Python 3.2,并且安装了lxml-2.3.win32-py3.2.exe


在Python2.7上它可以工作。

输出文件应该是二进制模式

outFile = open('output.xml', 'wb')

将二进制文件转换为base64,反之亦然。在python 3.5.2中验证

import base64

read_file = open('/tmp/newgalax.png', 'rb')
data = read_file.read()

b64 = base64.b64encode(data)

print (b64)

# Save file
decode_b64 = base64.b64decode(b64)
out_file = open('/tmp/out_newgalax.png', 'wb')
out_file.write(decode_b64)

# Test in python 3.5.2

我并没有对此进行真正的研究,但一个快速的猜测是您应该以二进制模式打开该文件。Python3重新设想了如何处理这个小“b”。它过去只会让Windows用户感到恼火,因为他们会忘记包含它(或者因为使用了stdio而无法包含)。现在,它可以在所有平台上骚扰Python用户。希望这样做是值得的。如果您正在解析文本,那么这肯定是值得的。@nobar需要关闭通用换行符支持,例如,在python3的gzip中,默认情况下在python3中对我来说也是打开的
json.load(gzip.open('file.json.gz'))
失败,
json.load(gzip.open('file.json.gz','rt'))
成功@LennartRegebro,如果系统设置意外,则不会。二进制是最好的,并且不容易出错。如果它起作用,它确实起作用。至于文本,总是有一个“如果”涉及。