builtins.TypeError: moet str zijn, niet bytes

Ik heb mijn scripts omgezet van Python 2,7-3,2, en ik heb een 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',
pageElement = etree.SubElement(page, 'City', 
                                      Storage='Basic',AnotherParameter = 'Hello')
# For multiple multiple attributes, use as shown above
# Save to XML file
outFile = open('output.xml', 'w')

Op de laatste regel, kreeg ik deze fout:

builtins.TypeError: must be str, not bytes
File "C:\PythonExamples\XmlReportGeneratorExample.py", line 29, in <module>
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)

Ik heb Python 3.2 geïnstalleerd en ik heb lxml-2.3.win32-py3.2.exe geïnstalleerd.

Op Python 2.7 werkt het.

Antwoord 1, autoriteit 100%

Het outfile moet in binaire modus staan.

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

Antwoord 2

Converteer binair bestand naar base64 & vice versa. Bewijs in 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')
# Test in python 3.5.2

Other episodes