My program saves a bit of XML data to a file in a prettyfied format from an XML string. This does the trick:
from xml.dom.minidom import parseString
dom = parseString(strXML)
with open(file_name + ".xml", "w", encoding="utf8") as outfile:
outfile.write(dom.toprettyxml())
However, I noticed that my XML header is missing an encoding parameter.
<?xml version="1.0" ?>
Since my data is susceptible of containing many Unicode characters, I must make sure UTF-8 is also specified in the XML encoding field.
Now, looking at the minidom documentation, I read that "an additional keyword argument encoding can be used to specify the encoding field of the XML header". So I try this:
from xml.dom.minidom import parseString
dom = parseString(strXML)
with open(file_name + ".xml", "w", encoding="utf8") as outfile:
outfile.write(dom.toprettyxml(encoding="UTF-8"))
But then I get:
TypeError: write() argument must be str, not bytes
Why doesn't the first piece of code yield that error? And what am I doing wrong?
Thanks!
R.