How to solve TypeError: cannot serialize float Python Elementtree



I got a debugging question.


Since I am quite new here, please forgive possible janky walls-of-text.


After many hours I finally got elementtree to do what I want, but I cannot output my results, because



tree.write("output3.xml")


as well as



print(ET.tostring(root))


give me


TypeError: cannot serialize 0.029999999999999999 (type float64)


I don't know what you guys need to help me out here, all the source code is sorta lengthy. So is the error message. But that's a little easier, so I post it here...


notes in advance:



  • As far as I can see and crtl-f I don't have that 0.029999999... in my data

  • All numerics are rounded to 2 decimals in my data

  • does rounding change anything at all btw? Or is it just for display?

  • I am really very confused by this, especially because there seem to be no googleable similar cases, just almost-but-not-entirely-enough ones.



--------------------------------------------------------------------------- TypeError Traceback (most recent call last) in () ----> 1 tree.write("output3.xml")


C:\Anaconda\lib\xml\etree\ElementTree.pyc in write(self, file_or_filename, encoding, xml_declaration, default_namespace, method) 818 ) 819 serialize = _serialize[method] --> 820 serialize(write, self._root, encoding, qnames, namespaces) 821 if file_or_filename is not file: 822 file.close()


C:\Anaconda\lib\xml\etree\ElementTree.pyc in _serialize_xml(write, elem, encoding, qnames, namespaces) 937 write(_escape_cdata(text, encoding)) 938 for e in elem: --> 939 _serialize_xml(write, e, encoding, qnames, None) 940 write("") 941 else:


C:\Anaconda\lib\xml\etree\ElementTree.pyc in _serialize_xml(write, elem, encoding, qnames, namespaces) 937 write(_escape_cdata(text, encoding)) 938 for e in elem: --> 939 _serialize_xml(write, e, encoding, qnames, None) 940 write("") 941 else:


C:\Anaconda\lib\xml\etree\ElementTree.pyc in _serialize_xml(write, elem, encoding, qnames, namespaces) 937 write(_escape_cdata(text, encoding)) 938 for e in elem: --> 939 _serialize_xml(write, e, encoding, qnames, None) 940 write("") 941 else:


C:\Anaconda\lib\xml\etree\ElementTree.pyc in _serialize_xml(write, elem, encoding, qnames, namespaces) 937 write(_escape_cdata(text, encoding)) 938 for e in elem: --> 939 _serialize_xml(write, e, encoding, qnames, None) 940 write("") 941 else:


C:\Anaconda\lib\xml\etree\ElementTree.pyc in _serialize_xml(write, elem, encoding, qnames, namespaces) 937 write(_escape_cdata(text, encoding)) 938 for e in elem: --> 939 _serialize_xml(write, e, encoding, qnames, None) 940 write("") 941 else:


C:\Anaconda\lib\xml\etree\ElementTree.pyc in _serialize_xml(write, elem, encoding, qnames, namespaces) 930 v = qnames[v.text] 931 else: --> 932 v = _escape_attrib(v, encoding) 933 write(" %s=\"%s\"" % (qnames[k], v)) 934 if text or len(elem):


C:\Anaconda\lib\xml\etree\ElementTree.pyc in _escape_attrib(text, encoding) 1090 return text.encode(encoding, "xmlcharrefreplace") 1091 except (TypeError, AttributeError): -> 1092 _raise_serialization_error(text) 1093 1094 def _escape_attrib_html(text, encoding):


C:\Anaconda\lib\xml\etree\ElementTree.pyc in _raise_serialization_error(text) 1050 def _raise_serialization_error(text): 1051 raise TypeError( -> 1052 "cannot serialize %r (type %s)" % (text, type(text).name) 1053 ) 1054


TypeError: cannot serialize 0.029999999999999999 (type float64)



No comments:

Post a Comment