使用 python 从 xml 中检索标签

问题描述 投票:0回答:1

我在employeedata.xml 文件中有员工数据。我的样本数据如下所示

 <?xml version="1.0" standalone="yes"?>
    <DocumentElement>
      <_x005B_dbo_x005D_._x005B_employeedata_x005D_>
        <RowID>11148</RowID>
        <ItemID>966109</ItemID>
        <Mappings>[]</Mappings>
        <Groups>93664,68349</Groups>
        <GroupKey>7003142</GroupKey>
        <ParentItemID>351908</ParentItemID>
        <JobID>30318</JobID>
        <Action>Employee</Action>
        <Employee_Name>John Travis</Employee_Name>
        <mail_id>[email protected]</mail_id>
        <...>...</...>  
        <Action>Experience</Action>
        <...>...</...>
        <...>...</...>
        <...>...</...>

我使用 python 3.8.10 来读取文件。我正在使用以下代码来读取我的数据

with open('employeedata.xml', 'r') as f:
    data = f.read()

我想将我的xml存储在pandas数据框中,其中标签

Action
下的值将存储在
Action
列中,相应的标签将存储在其他列
Field
中。我的示例输出看起来像

|    Action    |    Field    |
|--------------|-------------|
|    Employee  |Employee_Name|
|    Employee  |mail_id      |
|    Employee  |....         |
|    Employee  |....         |
|    Experience|....         |
|    Experience|....         |

您能建议我该怎么做吗?

python xml
1个回答
0
投票

您可能正在寻找这个 python xml 解析器模块

您的情况:

import xml.etree.ElementTree as ET
tree = ET.parse('employeedata.xml')
root = tree.getroot()
© www.soinside.com 2019 - 2024. All rights reserved.