gpt4 book ai didi

python - python 中的 xml2array

转载 作者:行者123 更新时间:2023-12-01 06:06:13 25 4
gpt4 key购买 nike

file.xml 包含以下数据结构:

<table>
<row>
<x>10</x>
<t>10:00</t>
</row>
<row>
<x>20</x>
<t>10:05</t>
</row>
</table>

我有数百个 x,t 对。我需要绘制 x 与 t 的关系图。如果您能帮助我解决 python 和 matplotlib 的问题,我将不胜感激。

最佳答案

import lxml.etree as ET
import matplotlib.pyplot as plt

text='''\
<table>
<row>
<x>10</x>
<t>10:00</t>
</row>
<row>
<x>20</x>
<t>10:05</t>
</row>
</table>
'''

def convert_t(text):
a,b=map(float,t.split(':'))
return a+b/60.0

doc=ET.fromstring(text)
x=[float(x) for x in doc.xpath('//x/text()')]
t=[convert_t(t) for t in doc.xpath('//t/text()')]
plt.plot(x,t)
plt.show()
  • 上面的代码假设 10:05意思是 10 分钟、5 分钟秒。它使用 convert_t 将其转换为 float .
  • 结束 </table>添加是为了使 XML 有效。

关于python - python 中的 xml2array,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7866337/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com