【问题标题】:How to write data from multiple xml tags into multiple columns in csv?如何将来自多个 xml 标签的数据写入 csv 中的多个列?
【发布时间】:2017-09-12 19:18:44
【问题描述】:

我正在尝试从返回 XML 对象的 API 调用中获取数据,并将少量数据点解析到一个 csv 文件中,每个对象都在其自己的列中。

XML 如下所示:

<?xml version="1.0" encoding="utf-8" ?>

<YourMembership_Response>
<Items>
<Item>
<ItemID></ItemID>
<ID>92304823A-2932</ID>
<WebsiteID>0987</WebsiteID>
<NamePrefix></NamePrefix>
<FirstName>John</FirstName>
<MiddleName></MiddleName>
<LastName>Smith</LastName>
<Suffix></Suffix>
<Nickname></Nickname>
<EmployerName>abc company</EmployerName>
<WorkTitle>manager</WorkTitle>
<Date>3/14/2013 2:12:39 PM</Date>
<Description>Removed from group by Administration.</Description>
</Item>
<Item>
<ItemID></ItemID>
<ID>92304823A-2932</ID>
<WebsiteID>0987</WebsiteID>
<NamePrefix></NamePrefix>
<FirstName>John</FirstName>
<MiddleName></MiddleName>
<LastName>Smith</LastName>
<Suffix></Suffix>
<Nickname></Nickname>
<EmployerName>abc company</EmployerName>
<WorkTitle>manager</WorkTitle>
<Date>3/14/2013 2:12:39 PM</Date>
<Description>Removed from group by Administration.</Description>
</Item>

我编写了这段代码,只将 ID 写入 CSV,效果很好。

with open("output1.csv", "wb") as f:
    writer = csv.writer(f)
    for node in tree.findall('.//ID'):
        writer.writerow([node.text])

现在,当我尝试将多个数据点写入 csv 时,机器只是将数据点附加到一列中。这是我一直在尝试的代码:

with open("test1.csv", "wb") as f:
    writer = csv.writer(f)
    for node in tree.findall('.//ID'):
        writer.writerow([node.text])
    for node in tree.findall('.//FirstName'):
        writer.writerow([node.text])
    for node in tree.findall('.//LastName'):
        writer.writerow([node.text]) 

我需要数据在 csv 中看起来像这样,稍后选择其他数据点,我做错了什么?:

ID                    FirstName     LastName
92304823A-2932         John           Smith

提前谢谢你。

【问题讨论】:

  • 输入的xml有多大?
  • 我没有输入大小的答案,但我必须为大约 15000 个成员这样做。

标签: python-2.7 xml-parsing elementtree


【解决方案1】:

本质上,这就是如何收集数据。

>>> from xml.etree import ElementTree
>>> tree = ElementTree.parse('api.xml')
>>> tree.findall('.//Item')
[<Element 'Item' at 0x0000000006679EA8>, <Element 'Item' at 0x0000000006681318>]
>>> for item in tree.findall('.//Item'):
...     item.find('ID').text, item.find('FirstName').text, item.find('LastName').text
... 
('92304823A-2932', 'John', 'Smith')
('92304823A-2932', 'John', 'Smith')

相比之下,当您使用像 tree.findall('.//ID') 这样的构造时,您要求 xpath 引擎以 tree 开头(这是“。”部分)并向下查看所有出现的“ID”的分支 一次。这意味着,在您的示例 xml 代码中,您将获得一个由两个 ID 组成的 set,它们甚至不一定是原始顺序。您需要做的是,首先找到所有Item 条目,然后找到与该Item 对应的三个感兴趣的数据。

附录:

>>> import csv
>>> with open('api.csv', 'w', newline='') as csvfile:
...     fieldnames = ['ID', 'FirstName', 'LastName']
...     writer = csv.DictWriter(csvfile, fieldnames=fieldnames)
...     writer.writeheader()
...     for item in tree.findall('.//Item'):
...         writer.writerow({
...             'ID': item.find('ID').text,
...             'FirstName': item.find('FirstName').text,
...             'LastName': item.find('LastName').text})

生成的输出文件:

ID,FirstName,LastName
92304823A-2932,John,Smith
92304823A-2932,John,Smith

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-09-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-11-21
    • 1970-01-01
    • 1970-01-01
    • 2018-04-02
    相关资源
    最近更新 更多