builtins.TypeError: must be str, not bytes(builtins.TypeError:必须是 str,而不是 bytes)
本文介绍了builtins.TypeError:必须是 str,而不是 bytes的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我已将我的脚本从 Python 2.7 转换为 3.2,但我遇到了一个错误.
I've converted my scripts from Python 2.7 to 3.2, and I have a 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:PythonExamplesXmlReportGeneratorExample.py", line 29, in <module>
doc.write(outFile)
File "c:Python32Libsite-packageslxmletree.pyd", line 1853, in lxml.etree._ElementTree.write (src/lxml/lxml.etree.c:44355)
File "c:Python32Libsite-packageslxmletree.pyd", line 478, in lxml.etree._tofilelike (src/lxml/lxml.etree.c:90649)
File "c:Python32Libsite-packageslxmletree.pyd", line 282, in lxml.etree._ExceptionContext._raise_if_stored (src/lxml/lxml.etree.c:7972)
File "c:Python32Libsite-packageslxmletree.pyd", line 378, in lxml.etree._FilelikeWriter.write (src/lxml/lxml.etree.c:89527)
我已经安装了 Python 3.2,并且我已经安装了 lxml-2.3.win32-py3.2.exe.
I've installed Python 3.2, and I've installed lxml-2.3.win32-py3.2.exe.
在 Python 2.7 上它可以工作.
On Python 2.7 it works.
推荐答案
输出文件应该是二进制模式.
The outfile should be in binary mode.
outFile = open('output.xml', 'wb')
这篇关于builtins.TypeError:必须是 str,而不是 bytes的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
沃梦达教程
本文标题为:builtins.TypeError:必须是 str,而不是 bytes


猜你喜欢
- 如何将一个类的函数分成多个文件? 2022-01-01
- python check_output 失败,退出状态为 1,但 Popen 适用于相同的命令 2022-01-01
- pytorch 中的自适应池是如何工作的? 2022-07-12
- 分析异常:路径不存在:dbfs:/databricks/python/lib/python3.7/site-packages/sampleFolder/data; 2022-01-01
- 如何在 python3 中将 OrderedDict 转换为常规字典 2022-01-01
- 使用Heroku上托管的Selenium登录Instagram时,找不到元素';用户名'; 2022-01-01
- 沿轴计算直方图 2022-01-01
- python-m http.server 443--使用SSL? 2022-01-01
- padding='same' 转换为 PyTorch padding=# 2022-01-01
- 如何在 Python 的元组列表中对每个元组中的第一个值求和? 2022-01-01