如何解决 TypeError: cannot serialize float Python Elementtree

新手上路,请多包涵

我有一个调试问题。

由于我在这里很新,请原谅可能出现的文字墙。

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

tree.write("output3.xml")

print(ET.tostring(root))

给我

类型错误:无法序列化 0.029999999999999999(类型 float64)

我不知道你们需要什么来帮助我,所有的源代码都有些冗长。错误信息也是如此。但这有点容易,所以我把它贴在这里……

提前注意事项:

  • 据我所知,Ctrl+FI 在我的数据中没有 0.029999999…
  • 我的数据中所有数字都四舍五入到小数点后两位
  • 顺便说一句,四舍五入会改变什么吗?还是只是为了展示?
  • 我真的对此感到非常困惑,尤其是因为似乎没有可通过谷歌搜索的类似案例,只有差不多但还不够多的案例。

---------------------------------------------- ---------------------- TypeError Traceback (最近一次调用) 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[方法] –> 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, 编码) 933 write(” %s=\”%s\”” % (qnames[k], v)) 934 如果文本或 len(elem):

C:\Anaconda\lib\xml\etree\ElementTree.pyc in _escape_attrib(text, encoding) 1090 return text.encode(encoding, “xmlcharrefreplace”) 1091 除了 (TypeError, AttributeError): -> 1092 _raise_serialization_error(text) 1093 1094 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 “无法序列化 %r (type %s)” % (text, type(文字). 姓名) 1053 ) 1054

类型错误:无法序列化 0.029999999999999999(类型 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 曲线由元组组成 (timepoint @ percentage of time elapsed, Pitch (in Hertz) @ timepoint)

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

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

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

我可能没有浮点数而使用字符串,但奇怪的是,我的 Dataframe 中的东西似乎是字符串,因为当我尝试对从那里提取的任何值应用 round() 函数时,它会抗议输入不是浮点数…

屏幕:

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

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

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

原文由 sibert 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 1k
2 个回答

你应该把你的问题归结为一个 简单的例子 这可能会帮助您自己解决问题,但更重要的是,现在阅读它的任何人基本上都必须猜测您的意图,因为您没有展示代码示例、输入或预期输出。

可能的问题是您将 ElementTree 属性或 text 的值设置为 Numpy float64 对象 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 本身及其大多数标准库都严格进行类型检查,不会自动从数字类型转换为字符串。

原文由 Dan Lenski 发布,翻译遵循 CC BY-SA 3.0 许可协议

(如上所述)只需将您添加到 ElementTree 中的浮点数转换为字符串即可。

 str(floatValue)

原文由 user163859 发布,翻译遵循 CC BY-SA 4.0 许可协议

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题