Got a Problem with generating a .SVG File with Python3 and ElementTree.
from xml.etree import ElementTree as et
doc = et.Element('svg', width='480', height='360', version='1.1', xmlns='http://www.w3.org/2000/svg')
#Doing things with et and doc
f = open('sample.svg', 'w')
f.write('<?xml version=\"1.0\" standalone=\"no\"?>\n')
f.write('<!DOCTYPE svg PUBLIC \"-//W3C//DTD SVG 1.1//EN\"\n')
f.write('\"http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd\">\n')
f.write(et.tostring(doc))
f.close()
The Function et.tostring(doc) generates the TypeError "write() argument must be str, not bytes". I don't understand that behavior, "et" should convert the ElementTree-Element into a string? It works in python2, but not in python3. What did i do wrong?
tostring
. Does that help? – Cardenopen()
as it should) writes to the file in text mode. ButElementTree.write()
wants binmode and indeedet.toString()
returns bytes (typing!). The encoding for what will be "XML text" must be given toElementTree.write()
instead! This is due to the XML header (which here is written in the second line, but should not) which is written byET.ElementTree
if'xml_declaration' = True
which contains the encoding's name! It think that is called "non-transparency of lower layers". – Instrumentation