python - 如何解决类型错误: cannot serialize float Python Elementtree

标签 python serialization pandas elementtree marytts

我有一个调试问题。

由于我是新来的,请原谅可能出现的困惑的文字墙。

几个小时后,我终于让 elementtree 做我想做的事,但我无法输出我的结果,因为

tree.write("output3.xml")

以及

print(ET.tostring(root))

给我

TypeError: cannot serialize 0.029999999999999999 (type float64)

我不知道你们需要什么帮助我,所有的源代码都很长。错误消息也是如此。但这更容易一些,所以我将其发布在这里......

提前说明:

  • 据我所知,按 Ctrl+F,我的数据中没有 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)

好的,首先进行编辑。我将粘贴我想要实现的本质的屏幕截图。

手头的任务是使用 python 与 pandas 和 elementtree 来更新 xml 文件。

该文件由文本转语音系统 MARY 输出,包含如何合成给定话语的信息。

该文件具有以下结构(简化)

<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...

这意味着对于“测试”一词中的任何给定电话/声音,XML 包含声音信息,顺序如下:声音类型、长度、时间终点、音高 (f0) 曲线。 f0 曲线由元组组成(时间点@耗时的百分比,音调(以赫兹为单位)@时间点)

从另一个程序 PRAAT 中,我获得了更新的计时和音调信息,存储在数据框中,请参阅其他屏幕截图。

我的 Python 解析 xml 并覆盖每个声音的声学信息。但随后无法输出。

答案中的初学者 float 链接使事情变得更加清晰。显然四舍五入根本没有帮助。

我可能可以在没有 float 的情况下使用字符串,但奇怪的是,我的数据帧中的东西似乎是字符串,因为当我尝试对从那里提取的任何值应用 round() 函数时,它会抗议输入是不是 float ...

屏幕:

是的。伟大的。需要更多的图像声誉。老鼠。所以只是链接。

http://puu.sh/bzQQr/6fed162db8.png

http://puu.sh/bzQNq/23490bfb63.png

最佳答案

您应该将问题归结为 simple example .这可能会帮助您自己解决问题,但更重要的是,现在任何阅读它的人基本上都必须猜测您的意图,因为您还没有显示代码、输入或预期输出的示例.

问题可能在于您将 ElementTree 属性或 text 的值设置为 Numpy float64 object 。 ElementTree 库不知道 float64 类型,也不会尝试将其静默转换为字符串。

例如,您的代码中可能有类似的内容(我不知道您的代码到底是如何工作的,因为您没有展示它):

# 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)

您应该将其替换为:

et.find(".//element").text = str(float64(0.3))

Python 本身及其大多数标准库对类型检查都很严格,不会自动从数字类型转换为字符串。

关于python - 如何解决类型错误: cannot serialize float Python Elementtree,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25839855/

相关文章:

python - Pandas 数据帧 : Can I fetch other column values along with the column on which group by clause has been applied?

python - 如何在 Python 中创建返回此列表的所有组合的函数?

python - 从 Python 中的正则表达式模式获取多个匹配项

python - Ckan 安装失败,出现 : Unknown distribution option: 'message_extractors'

C# 部分反序列化

python - pd.DataFrame.select_dtypes() 包括 timedelta dtype

python - Python 中的列表索引

java - 如何使用 GSON 有效地延迟加载 JSON 文件的特定元素?

C++ Boost 对象序列化 - 定期保存以保护数据

python - 使用 matplotlib 绘图时如何排序分类月份变量?