title: How to solve TypeError: cannot serialize float Python Elementtree
body: <p>I got a debugging question.</p>
  
  <p>Since I am quite new here, please forgive possible janky walls-of-text.</p>
  
  <p>After many hours I finally got <code>elementtree</code> to do what I want, but I cannot output my results, because</p>
  
  <pre><code>tree.write("output3.xml")
  </code></pre>
  
  <p>as well as </p>
  
  <pre><code>print(ET.tostring(root))
  </code></pre>
  
  <p>gives me</p>
  
  <blockquote>
    <p>TypeError: cannot serialize 0.029999999999999999 (type float64)</p>
  </blockquote>
  
  <p>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... </p>
  
  <p>notes in advance:</p>
  
  <ul>
  <li>As far as I can see and Ctrl+F I don't have that 0.029999999... in my data</li>
  <li>All numerics are rounded to 2 decimals in my data</li>
  <li>does rounding change anything at all btw? Or is it just for display?</li>
  <li>I am really very confused by this, especially because there seem to be no googleable similar cases, just almost-but-not-entirely-enough ones. </li>
  </ul>
  
  <blockquote>
    <p>--------------------------------------------------------------------------- TypeError                                 Traceback (most recent call
    last)  in ()
    ----> 1 tree.write("output3.xml")</p>
    
    <p>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()</p>
    
    <p>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:</p>
    
    <p>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:</p>
    
    <p>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:</p>
    
    <p>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:</p>
    
    <p>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:</p>
    
    <p>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):</p>
    
    <p>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):</p>
    
    <p>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).<strong>name</strong>)    1053         )    1054 </p>
    
    <p>TypeError: cannot serialize 0.029999999999999999 (type float64)</p>
  </blockquote>
  
  <p><strong>Okay, first edits first.</strong> I will paste screenshots of the essences I am trying to achieve.</p>
  
  <p>The task at hand is using python with pandas and <code>elementtree</code> to update an xml file.</p>
  
  <p>The file is outputted by the Text-To-Speech system MARY and contains information how to synthesize a given utterance.</p>
  
  <p>That file has the following structure (simplified)</p>
  
  <pre><code>&lt;phrase&gt;
  &lt;word&gt;
  &lt;syllable = "t e s t"&gt;
  &lt;phone = "t" duration = "30" end = "230"&gt;
  &lt;phone = "e" duration = "90" end = "320" f0 = "(25,144)(50,145)(75,150)(100,149)"&gt;
  ...and so on...see screenshot for details...
  </code></pre>
  
  <p>this means that for any given phone/sound in the word "test" the XML contains acoustic information, in this order: Type of sound, length, endpoint in time, pitch (f0) curve.
  The f0 curve consists of tuples (timepoint @ percentage of time elapsed, Pitch (in Hertz) @ timepoint)</p>
  
  <p>From another program, PRAAT, I obtained updated timing and pitch information, stored in a dataframe, see other screenshot. </p>
  
  <p>My Python parses the xml and overwrites the acoustic info for each sound. But then fails to output.</p>
  
  <p>The float-for-beginners link in the answer made things a little clearer. Apparently rounding does not help at all. </p>
  
  <p>I could possibly live without floats and use strings, but curiously the things in my Dataframe appear to BE strings, since when I try to apply the round() function on any value extracted from there, it will protest that the input is not a float...</p>
  
  <p>screenys:</p>
  
  <p>ya. great. need more reputation for images. rats. so just links.</p>
  
  <p><a href="http://puu.sh/bzQQr/6fed162db8.png" rel="noreferrer">http://puu.sh/bzQQr/6fed162db8.png</a></p>
  
  <p><a href="http://puu.sh/bzQNq/23490bfb63.png" rel="noreferrer">http://puu.sh/bzQNq/23490bfb63.png</a></p>