Can xml.etree.ElementTree.write () be integer values ​​for this element?

at the risk of screaming at asking such a simple question, but I’m baiting the Internet for answers, and this particular case seems to be widely avoided, and the documents are ambiguous:

Is it possible to use xml.etree.ElementTree.write () to write non-string values ​​in an element attribute? I always get:

TypeError: cannot serialize 0 (type int) 

when I try something like this:

 root = ET.Element('Tasks') d = {'priority': 1, 'status': 0, 'name': 'new task', 'index': 0} d = ET.SubElement(root, 'Settings', attrib=d) tree = ET.ElementTree(root) tree.write('/tmp/xmlTest') 

I worked on it several times, iterating over the corresponding dictionary and first turning all the values ​​into strings, but this does not seem to be correct, and before I process it again, I would like to know how it should be done correctly so as not to get used to the bad habit. So any insight would be greatly appreciated.

Cheers frank

+6
source share
1 answer

Unlike XML data, XML attributes are texts. http://www.w3schools.com/xml/xml_attributes.asp

It is up to you to serialize the attributes for strings before serializing the XML.

+8
source

Source: https://habr.com/ru/post/956526/


All Articles