【问题标题】:How to convert this XML file to CSV using python?如何使用 python 将此 XML 文件转换为 CSV?
【发布时间】:2021-07-31 12:26:48
【问题描述】:

特此附上我要转换为 csv 的 xml 文件

<?xml version="1.0"?>
<response>
  <data>
    <shops>
      <shop id="204019">
        <name>Bannockburn</name>
        <status>Open</status>
        <company id="25">Franchise</company>
        <shopAttributes>
          <shopAttribute attrName="shop_OPEN_DATE">2008-07-16</shopAttribute>
          <shopAttribute attrName="CLOSE_DATE"/>
          <shopAttribute attrName="shop_DISTRIBUTION_CTR_GENERAL" startDate="2019-03-19">90</shopAttribute>
          <shopAttribute attrName="shop_DISTRIBUTION_CTR_GENERAL" startDate="1900-01-01" endDate="2019-03-18"/>
        </shopAttributes>
        <addresses>
          <address type="PUBLIC">
            <addressLine1>1211 Half Day Road</addressLine1>
            <addressLine2></addressLine2>
            <city>Bannockburn</city>
            <stateProvince>IL</stateProvince>
            <postalCode>60015</postalCode>
            <country>USA</country>
            <latitude>42.199461</latitude>
            <longitude>-87.860582</longitude>
          </address>
        </addresses>
      </shop>
      <shop id="204020">
        <name>Plainfield - North Plainfield</name>
        <status>Open</status>
        <company id="25">Franchise</company>
        <shopAttributes>
          <shopAttribute attrName="shop_OPEN_DATE">2007-05-18</shopAttribute>
          <shopAttribute attrName="CLOSE_DATE"/>
          <shopAttribute attrName="shop_DISTRIBUTION_CTR_GENERAL" startDate="2019-03-19">90</shopAttribute>
          <shopAttribute attrName="shop_DISTRIBUTION_CTR_GENERAL" startDate="1900-01-01" endDate="2019-03-18"/>
        </shopAttributes>
        <addresses>
          <address type="PUBLIC">
            <addressLine1>12632 IL Route 59</addressLine1>
            <addressLine2>Suite #102</addressLine2>
            <city>Plainfield</city>
            <stateProvince>IL</stateProvince>
            <postalCode>60585</postalCode>
            <country>USA</country>
            <latitude>41.653125</latitude>
            <longitude>-88.204527</longitude>
          </address>
        </addresses>
      </shop>
</shops>
</data>
</response>

这是我要转换为 csv 的 xml 文件,有人可以帮我在 python 中如何做吗? 下面是我尝试使用的代码,但我还没有真正理解如何去做,看到了一些例子,但不是很清楚

from xml.etree import ElementTree

tree = ElementTree.parse('Store.xml')
root = tree.getroot()

for att in root:
    first = att.find('shops').text
    print('{}'.format(first))

但我这里没有。

【问题讨论】:

  • 谷歌:即看看这里:blog.appliedinformaticsinc.com/…
  • @SiebeJongebloed 我见过这个,当我尝试使用它时。我得到了一个空的 csv,所以我认为我一定缺少一些东西。
  • 您想获取什么信息? Shops 没有文字,因此只会打印空白。

标签: python xml csv xpath elementtree


【解决方案1】:

Shops 没有文本,因此它不会打印任何内容。你需要达到你想要的水平

for att in root.findall('./data/shops/shop’):
    first = att.find('name')
    print('{}'.format(first.text))

给予

Bannockburn
Plainfield - North Plainfield

这里有一个很好的 ElementTree 资源:https://docs.python.org/3/library/xml.etree.elementtree.html

【讨论】:

    【解决方案2】:

    不是一个完整的解决方案,但要回答您为什么获得None,这是因为您的商店实际上更深一层,在data 标签下。

    这段代码可能会让您了解如何访问底层属性,您可以将其收集到列表或其他容器中以构建您的 CSV。

    from xml.etree import ElementTree
    
    tree = ElementTree.parse('Store.xml')
    root = tree.getroot()
    data = root.find('data')
    
    for shops in data:
        for shop in shops:
            name = shop.find('name').text
            sid = shop.attrib
            status = shop.find('status').text
            attrs = shop.find('shopAttributes')
            open_date = attrs.find(".//shopAttribute/[@attrName='shop_OPEN_DATE']").text
            print(f"Name: {name}, ID: {sid}, Status: {status}, open: {open_date}")
    

    open_date 是如何使用XPath 访问属性的示例。代码返回:

    Name: Bannockburn, ID: {'id': '204019'}, Status: Open, open: 2008-07-16
    Name: Plainfield - North Plainfield, ID: {'id': '204020'}, Status: Open, open: 2007-05-18
    

    【讨论】:

    • 解释以及附加的链接真的很有帮助。非常感谢!
    • 没有问题!我记得回溯元素树的时候……这是一个很好的评论!如果您想要更复杂的行为,可能会有更好的方法来做到这一点。 =)
    猜你喜欢
    • 2019-12-01
    • 2021-05-08
    • 2021-11-21
    • 1970-01-01
    • 2016-01-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多