gpt4 book ai didi

python - 使用 Python 和 LXML 从数据框创建 XML 文件

转载 作者:行者123 更新时间:2023-12-04 16:04:09 26 4
gpt4 key购买 nike

我正在尝试创建一个 XML 文件,该文件使用 Pandas 数据框来填充元素和子元素。这是我写的代码:

import pandas as pd
from lxml import etree as et

df = pd.DataFrame({'id_profile': [439, 444654, 56454, 12222], 'ServiceDate':
['2017-12-05', '2017-01-25', '2017-12-05', '2017-01-25'],
'PrimaryServiceCategory': [25, 25, 33, 25]})

root = et.Element('ClientReport')
idnum = et.SubElement(root, 'ID')
prime_serv = et.SubElement(root, 'ServiceCategory')
serv_date = et.SubElement(root, 'ServiceDate')

for row in df.iterrows():
idnum.text = df['id_profile']
prime_serv.text = df['PrimaryServiceCategory']
serv_date.text = df['ServiceDate']

print(et.tostring(root, pretty_print=True))

我的预期结果是:

<ClientReport>
<ID>439</ID>
<ServiceCategory>25</ServiceCategory>
<ServiceDate>2017-12-05</ServiceDate>
</ClientReport>
<ClientReport>
<ID>444654</ID>
<ServiceCategory>25</ServiceCategory>
<ServiceDate>2017-01-25</ServiceDate>
</ClientReport>
<ClientReport>
<ID>12222</ID>
<ServiceCategory>25</ServiceCategory>
<ServiceDate>2017-01-25</ServiceDate>
</ClientReport>

相反,我得到:

TypeError: Argument must be bytes or unicode, got 'Series'

我不确定如何获取迭代行而不是静态值来填充 XML 文件的属性。 Pandas 是这里合适的包裹吗? LXML 也是正确的吗?

最佳答案

一种解决方案是遍历数据框,分别填充每个 XML 元素:

import pandas as pd
from lxml import etree as et

df = pd.DataFrame({'id_profile': [439, 444654, 56454, 12222], 'ServiceDate':
['2017-12-05', '2017-01-25', '2017-12-05', '2017-01-25'],
'PrimaryServiceCategory': [25, 25, 33, 25]})

root = et.Element('root')

for row in df.iterrows():
report = et.SubElement(root, 'ClientReport')
idnum = et.SubElement(report, 'ID')
prime_serv = et.SubElement(report, 'ServiceCategory')
serv_date = et.SubElement(report, 'ServiceDate')
idnum.text = str(row[1]['id_profile'])
prime_serv.text = str(row[1]['PrimaryServiceCategory'])
serv_date.text = str(row[1]['ServiceDate'])

print(et.tostring(root, pretty_print=True).decode('utf-8'))

关于python - 使用 Python 和 LXML 从数据框创建 XML 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49439008/

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