Python:将xml展平为csv,父标签在子标签中重复 [英] Python : Flatten xml to csv with parent tag repeated in child

查看:28
本文介绍了Python:将xml展平为csv,父标签在子标签中重复的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的 xml 如下所示.

My xml looks like below.

<RootTag xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<Branch>
<BranchID>100</BranchID>
   <Child>
        <policy1>hello</policy1>
        <policy2>how are you</policy2>
   </Child>
   <Child>
        <policy1>hello1</policy1>
        <policy2>how are you1</policy2>
   </Child>
</Branch>
<Branch>
<BranchID>200</BranchID>
<Child>
        <policy1>I am good</policy1>
        <policy2>how about you</policy2>
</Child>
<Child>
        <policy1>I am good1</policy1>
        <policy2>how about you1</policy2>
</Child>
</Branch>
</RootTag>

我尝试使用 getiterator(因为我使用的是 python 2.6)来获取所有元素.我可以将它们展平,但父标签 (BranchID) 值应该存在于其子项的所有行中.

I tried using getiterator(beacuse I'm in python 2.6) to get all the elements. I am able to flatten them but the parent tag(BranchID) values should be present in all the rows of its child.

BranchID,policy1,policy2
100,hello,how are you
100,hello1,how are you1
200,I am good,how about you
200,I am good1,how about you1

推荐答案

请试试这个代码(记得用实际的文件路径替换YOUR FILE HERE):

please try this code (remember to replace YOUR FILE HERE with an actual file path):

import xml.etree.ElementTree as ET

root = ET.parse('YOUR FILE HERE')  # replace file name

print("BranchID,policy1,policy2")
branches = root.findall('.//Branch')
for branch in branches:
    branch_id = branch.find("BranchID").text
    for child in branch.findall('.//Child'):
        policy1 = child.find('policy1').text
        policy2 = child.find('policy2').text
        print("{},{},{}".format(branch_id,policy1,policy2))

这篇关于Python:将xml展平为csv,父标签在子标签中重复的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆