【问题标题】:Read an xlsx file and write xml读取 xlsx 文件并写入 xml
【发布时间】:2021-09-11 11:38:40
【问题描述】:

我需要你的帮助,我正在尝试编写 python 代码,在输入中我使用 xlsx 我读取单元格的各个字段,然后生成一个 xml。我在读取日期时遇到了麻烦,所以我使用了 pandas,所以我可以使用数据框,现在你给她一把,我也读取了测试数据,但我没有成功在 XML 中写入这些数据;你能帮帮我吗?

import pandas as pd
import datetime
import json
import datetime
from xml.etree import ElementTree as ET

df = pd.read_excel('parser.xlsx') #leggo il file excel
df['data autorizzazio'] = pd.to_datetime(df['data autorizzazio'])
#df['data movimentazio'] = pd.to_datetime(df['data menter code hereovimentazio'])

#df.head()
#df.info()


ET.register_namespace("CBIPaymentRequest","http://www.w3.org/2001/XMLSchema-instance")

root = ET.Element("{http://www.w3.org/2001/XMLSchema-instance}CBIPaymentRequest")
root1 = ET.SubElement(root,"GrpHdr") 
#root2 = ET.SubElement(root,"PmtInf")
 
MsgId = ET.SubElement(root1,'MsgId')
MsgId = df.loc[0].values[1]#setto il valore della cella interessata
MsgId.text = df['data autorizzazio'].values[1]
#MsgId = MsgId
print(MsgId)

Prova = ET.SubElement(root1,'PROVA')
Prova = df.loc[0].values[5]
Prova1 = df.__setitem__(Prova,'Prova')
#Prova.text = df['Saluto5'].values[1]
print(Prova)

tree = ET.ElementTree(root)
tree.write("pandas_output_test_1.xml")

【问题讨论】:

标签: python pandas dataframe elementtree


【解决方案1】:

目前我生成了这个 xml(很遗憾是空的)

<CBIPaymentRequest:CBIPaymentRequest xmlns:CBIPaymentRequest="http://www.w3.org/2001/XMLSchema-instance">
     <GrpHdr>
       <MsgId/>
       <PROVA/>
     </GrpHdr>
</CBIPaymentRequest:CBIPaymentRequest>

我希望它填充了我在 xlsx 文件中读取的数据

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-03-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多