简体   繁体   English

builtins.TypeError:必须是str,而不是字节

[英]builtins.TypeError: must be str, not bytes

I've converted my scripts from Python 2.7 to 3.2, and I have a bug. 我已经将我的脚本从Python 2.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) 

On the last line, I got this error: 在最后一行,我收到了这个错误:

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)

I've installed Python 3.2, and I've installed lxml-2.3.win32-py3.2.exe. 我已经安装了Python 3.2,并且我安装了lxml-2.3.win32-py3.2.exe。

On Python 2.7 it works. 在Python 2.7上它可以工作。

outfile应该是二进制模式。

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

Convert binary file to base64 & vice versa. 将二进制文件转换为base64,反之亦然。 Prove in python 3.5.2 在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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 builtins.TypeError 类型错误 - builtins.TypeError TypeError 如何解决此错误的builtins.TypeError:预期的str,字节或os.PathLike对象,而不是builtin_function_or_method - How to fix this errror builtins.TypeError: expected str, bytes or os.PathLike object, not builtin_function_or_method builtins.TypeError:-:“ int”和“ str”的不受支持的操作数类型 - builtins.TypeError: unsupported operand type(s) for -: 'int' and 'str' TypeError:必须为str,而不是字节 - TypeError: must be str, not bytes Flask SQLAlchemy - sqlalchemy.exc.StatementError:(builtins.TypeError)序列项0:预期的str实例,找到元组 - Flask SQLAlchemy - sqlalchemy.exc.StatementError: (builtins.TypeError) sequence item 0: expected str instance, tuple found Builtins.TypeError:读取文件对象必须返回纯字符串:Xpath中的错误-python - builtins.TypeError: reading file objects must return plain strings : Error in Xpath - python builtins.TypeError:列表索引必须是整数,而不是list。 第10行,在 <module> 如果generation [row] [col] == 0: - builtins.TypeError: list indices must be integers, not list. line 10, in <module> if generation[row][col] == 0: builtins.TypeError: &#39;Button&#39; 对象不可调用 - builtins.TypeError: 'Button' object is not callable builtins.TypeError:“ NoneType”对象在Twisted中不可调用 - builtins.TypeError: 'NoneType' object is not callable in Twisted TypeError:必须是str,而不是字节Error - TypeError: must be str, not bytes Error
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM