如何解决TypeError:无法序列化浮动Python Elementtree
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 
原文地址: http://stackoverflow.com/questions/25839855/
Warning: these are provided under cc-by-sa 4.0 license.  You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to solve TypeError: cannot serialize float Python Elementtree
提问by sibert
I got a debugging question.
我有一个调试问题。
Since I am quite new here, please forgive possible janky walls-of-text.
由于我在这里很新,请原谅可能的笨拙的文字墙。
After many hours I finally got elementtreeto do what I want, but I cannot output my results, because
几个小时后,我终于elementtree可以做我想做的事,但我无法输出结果,因为
tree.write("output3.xml")
as well as
也
print(ET.tostring(root))
gives me
给我
TypeError: cannot serialize 0.029999999999999999 (type float64)
类型错误:无法序列化 0.029999999999999999(类型 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 Ctrl+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.
- 据我所知,Ctrl+FI 在我的数据中没有那个 0.029999999...
- 我的数据中的所有数字都四舍五入到小数点后两位
- 顺便说一句,舍入会改变什么吗?还是只是为了展示?
- 我对此感到非常困惑,尤其是因为似乎没有可搜索的类似案例,只有几乎但不完全足够的案例。
--------------------------------------------------------------------------- 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)
-------------------------------------------------- ------------------------- 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 如果 file_or_filename 不是文件: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 如果是 text 或 len(elem):
C:\Anaconda\lib\xml\etree\ElementTree.pyc in _escape_attrib(text, encoding) 1090 返回 text.encode(encoding, "xmlcharrefreplace") 1091 除了 (TypeError, AttributeError): -> 1092 _raise_serialization_error(text) 10493 10 def _escape_attrib_html(文本,编码):
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(文本)名称)1053)1054
类型错误:无法序列化 0.029999999999999999(类型 float64)
Okay, first edits first.I will paste screenshots of the essences I am trying to achieve.
好的,先编辑一下。我将粘贴我试图实现的本质的屏幕截图。
The task at hand is using python with pandas and elementtreeto update an xml file.
手头的任务是使用 python 和 pandas 并elementtree更新 xml 文件。
The file is outputted by the Text-To-Speech system MARY and contains information how to synthesize a given utterance.
该文件由 Text-To-Speech 系统 MARY 输出,并包含如何合成给定话语的信息。
That file has the following structure (simplified)
该文件具有以下结构(简化)
<phrase>
<word>
<syllable = "t e s t">
<phone = "t" duration = "30" end = "230">
<phone = "e" duration = "90" end = "320" f0 = "(25,144)(50,145)(75,150)(100,149)">
...and so on...see screenshot for details...
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)
这意味着对于单词“test”中的任何给定音素/声音,XML 包含声学信息,按以下顺序:声音类型、长度、时间终点、音高 (f0) 曲线。f0 曲线由元组组成(时间点@经过时间的百分比,音高(以赫兹为单位)@时间点)
From another program, PRAAT, I obtained updated timing and pitch information, stored in a dataframe, see other screenshot.
从另一个程序 PRAAT,我获得了更新的时间和音高信息,存储在数据帧中,请参阅其他屏幕截图。
My Python parses the xml and overwrites the acoustic info for each sound. But then fails to output.
我的 Python 解析 xml 并覆盖每个声音的声学信息。但随后无法输出。
The float-for-beginners link in the answer made things a little clearer. Apparently rounding does not help at all.
答案中的初学者浮动链接使事情变得更加清晰。显然四舍五入根本没有帮助。
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...
我可能没有浮点数而使用字符串,但奇怪的是,我的 Dataframe 中的东西似乎是字符串,因为当我尝试对从那里提取的任何值应用 round() 函数时,它会抗议输入不是浮点数...
screenys:
屏幕:
ya. great. need more reputation for images. rats. so just links.
是的 伟大的。需要更多的图像声誉。老鼠。所以只是链接。
http://puu.sh/bzQQr/6fed162db8.png
http://puu.sh/bzQQr/6fed162db8.png
采纳答案by Dan Lenski
You should boil your problem down to a simple example.This may help you solve the problem on your own, but more importantly, anyone who reads it now basically has to guess at your intentions since you haven't showed examples of your code, the input, or the intended output.
你应该把你的问题归结为一个简单的例子。这可能会帮助您自己解决问题,但更重要的是,现在阅读它的任何人基本上都必须猜测您的意图,因为您没有展示代码、输入或预期输出的示例。
Likely the problem is that you are setting the value of an ElementTreeattribute or textto a Numpy float64object. The ElementTree library doesn't know about the float64type and won't try to silently convert it to a string.
可能的问题是您正在设置一个ElementTree属性的值或text一个Numpyfloat64对象。ElementTree 库不知道float64类型,也不会尝试将其静默转换为字符串。
For example, you may have something like this in your code (I have no idea exactly how your code works since you haven't shown it):
例如,你的代码中可能有这样的东西(我不知道你的代码是如何工作的,因为你没有展示它):
# the value 0.3 cannot be exactly represented in floating points
# read this for starters: https://docs.python.org/3/tutorial/floatingpoint.html
et.find(".//element").text = float64(0.3)
You should replace it with this:
你应该用这个替换它:
et.find(".//element").text = str(float64(0.3))
Python itself, and most of its standard libraries, are strict about type-checking and will not automatically convert from numeric types to strings.
Python 本身及其大多数标准库对类型检查都很严格,不会自动从数字类型转换为字符串。

