Parse XML to Table in Python

2019-09-26 08:11发布

问题:

I'm trying to parse XML to table-like structure in Python. Imagine XML like this:

<?xml version="1.0" encoding="UTF-8"?>
<base>
  <element1>element 1</element1>
  <element2>element 2</element2>
  <element3>
    <subElement3>subElement 3</subElement3>
  </element3>
</base>

I'd like to have result like this:

KEY                       | VALUE
base.element1             | "element 1"
base.element2             | "element 2"
base.element3.subElement3 | "subElement 3"

I've tried using xml.etree.cElementTree, then functions described here How to convert an xml string to a dictionary in Python?

Is there any function that can do this? All answers I found are written for particular XML schemes and would need to be edited for each new XML scheme. For reference, in R it's easy with XML and XML2 packages and xmlToList function.

回答1:

I've got the needed outcome using following script.

XML File:

<?xml version="1.0" encoding="UTF-8"?>
<base>
  <element1>element 1</element1>
  <element2>element 2</element2>
  <element3>
    <subElement3>subElement 3</subElement3>
  </element3>
</base>

Python code:

import pandas as pd
from lxml import etree

data = "C:/Path/test.xml"

tree = etree.parse(data)

lstKey = []
lstValue = []
for p in tree.iter() :
    lstKey.append(tree.getpath(p).replace("/",".")[1:])
    lstValue.append(p.text)

df = pd.DataFrame({'key' : lstKey, 'value' : lstValue})
df.sort_values('key')

Result: