스크립트를 Python 2.7에서 3.2로 변환했으며 버그가 있습니다.
# -*- 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)
Python 3.2를 설치했고 lxml-2.3.win32-py3.2.exe를 설치했습니다.
파이썬 2.7에서는 작동합니다.
10
실제로 이것을 조사하지는 않았지만 빠른 추측은 파일을 바이너리 모드로 열어야한다는 것입니다.
—
Sven Marnach