I'm trying to parse XML to table-like structure in Python.
Imagine XML like this:
element 1
element 2
subElement 3
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.
解决方案
I've got the needed outcome using following script.
XML File:
element 1
element 2
subElement 3
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: