[英]Python extract XML data with inconsistant child tags
我有一个 XML 文件,我需要从中提取数据并插入到数据库表中。 我的挣扎是 XML 数据结构可能包含不一致的子标签。 这意味着(在下面的示例中)一个父<Field>
标签可能包含也可能不包含<ListValue>
标签。
这是一个简短的示例,我将添加可能包含另一个<ListValue>
标记的附加<Field>
标记。 注意:所有<Field>
标记都应保持在<Record>
标记下方的同一级别。
我想看看是否有人有比我下面的示例更“pythonic”的方式来转换这些数据。 也许与列表理解?
我需要将多达 4,000,000 个<Record>
级别的数据行插入到数据库中,因此我不想浪费更多时间在 XML 中进行不必要的循环。 速度将是必不可少的。
任何帮助将不胜感激。
<?xml version="1.0" encoding="utf-16"?>
<Records count="10">
<Metadata>
<FieldDefinitions>
<FieldDefinition id="15084" guid="f3426157-cbcb-4293-94e5-9f1c993db4b5" name="CCR_ID" alias="CCR_ID" />
<FieldDefinition id="16335" guid="5dfddb49-9a7a-46ee-9bd2-d5bbed97a48d" name="Coming Due" alias="Coming_Due" />
</FieldDefinitions>
</Metadata>
<LevelCounts>
<LevelCount id="35" guid="661c747f-7ce5-474a-b320-044aaec7a5b1" count="10" />
</LevelCounts>
<Record contentId="20196771" levelId="35" levelGuid="661c747f-7ce5-474a-b320-044aaec7a5b1" moduleId="265" parentId="0">
<Field id="15084" guid="f3426157-cbcb-4293-94e5-9f1c993db4b5" type="1">100383-320-V0217111</Field>
<Field id="16335" guid="5dfddb49-9a7a-46ee-9bd2-d5bbed97a48d" type="4">
<ListValues>
<ListValue id="136572" displayName="121 - 180 days out">121 - 180 days out</ListValue>
</ListValues>
</Field>
</Record>
<Record contentId="20205193" levelId="35" levelGuid="661c747f-7ce5-474a-b320-044aaec7a5b1" moduleId="265" parentId="0">
<Field id="15084" guid="f3426157-cbcb-4293-94e5-9f1c993db4b5" type="1">100383-320-V0217267</Field>
<Field id="16335" guid="5dfddb49-9a7a-46ee-9bd2-d5bbed97a48d" type="4">
<ListValues>
<ListValue id="136572" displayName="121 - 180 days out">121 - 180 days out</ListValue>
</ListValues>
</Field>
</Record>
<Record contentId="20196779" levelId="35" levelGuid="661c747f-7ce5-474a-b320-044aaec7a5b1" moduleId="265" parentId="0">
<Field id="15084" guid="f3426157-cbcb-4293-94e5-9f1c993db4b5" type="1">100384-320-V0217111</Field>
<Field id="16335" guid="5dfddb49-9a7a-46ee-9bd2-d5bbed97a48d" type="4">
<ListValues>
<ListValue id="136572" displayName="121 - 180 days out">121 - 180 days out</ListValue>
</ListValues>
</Field>
</Record>
</Records>
这是我解析数据的代码:
from xml.etree import ElementTree
import pandas as pd
xml_string = '''SEE STRING ABOVE'''
auth_token = ElementTree.fromstring(xml_string.text)
dct = []
cols = ['CCR_ID', 'Coming_Due']
for r in auth_token.findall("Record"):
for f in r.findall("Field"):
if f.attrib['id'] == '15084':
ccr_id = f.text
for l in f.findall(".//ListValue"):
coming_due = l.text
dct.append((ccr_id, coming_due))
df = pd.DataFrame(dct)
df.columns = cols
print(df)
这是我的结果:
CCR_ID Coming_Due
0 100383-320-V0217111 121 - 180 days out
1 100383-320-V0217267 121 - 180 days out
2 100384-320-V0217111 121 - 180 days out
3 100384-320-V0217267 121 - 180 days out
4 100681-320-V0217111 121 - 180 days out
5 100681-320-V0217267 11 - 30 days out
6 100684-320-V0217111 121 - 180 days out
7 100684-320-V0217267 11 - 30 days out
8 100685-320-V0217111 121 - 180 days out
9 100685-320-V0217267 11 - 30 days out
如果我理解正确,使用 pandas read_xml()
可能会有所帮助:
df = pd.read_xml(string,"//Record//*")
df2= df[['Field','displayName']].copy()
df2['displayName'] = df2['displayName'].shift(-3)
df2.set_axis(['CCR_ID', 'Coming_Due'], axis=1,inplace=True)
df2.dropna()
Output 基于您的样品 xml:
Field displayName
0 100383-320-V0217111 121 - 180 days out
4 100383-320-V0217267 121 - 180 days out
8 100384-320-V0217111 121 - 180 days out
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.