Parse XML to Table in Python

2024/11/18 6:36:18

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.

Answer

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 etreedata = "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:

Python result

https://en.xdnf.cn/q/120103.html

Related Q&A

Use Artificial Intelligence to predict next number (n+1) in a sequence

The AI must predict the next number in a given sequence of incremental integers using Python, but so far I havent gotten the intended result. I tried changing the learning rate and iterations but so fa…

Calculating size folder python [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.Want to improve this question? Update the question so it focuses on one problem only by editing this post.Closed 8…

Answer Error, Only outputting Zero

I am coding in python and I cannot seem to figure out why when the amount of tickets sold is entered it does not calculate the full price of the tickets that were sold. Any help is appreciated, Thanks.…

finding a pattern match and concatenating the rest of lines in python

I have a small data set to clean. I have opened the text file in Pycharm. The data set is like this:Code-6667+ Name of xyz company+ Address + Number+ Contact person+ Code-6668+ Name of abc company, A…

Flat a list of containing only one dict

I have a dict which contain a list product which will contain only one dict: d = {"thickness": 90.0,"mass_surf": 37.8,"res_therm": 0.75,"codename": "codenam…

How to breakup a list of list in a given way in Python [closed]

Its difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying thi…

Incrementing a counter while assigning it in python

Is it possible to do the following in python somehow?nodename = node%s % (++num if ct[0] != J else num)Or do I need to first do the increment and the assign it on the next line:if ct[0] != J:num += 1n…

Append two arrays together into one? (Numpy/Python)

I currently have an array of strings and Im trying to join it together with another array of strings to form a complete word to do some web parsing. For example:`Var1 [A B C, .... ....] Var2 …

Python 2.7 Isolate multiple JSON objects in a string

Ive to parse a text file that contains different kind of data. The most challenging is a line that contains three different JSON object (strings) and other data between them. Ive to divide the Json da…

pass different C functions with pointer arrays as the function argument to a class

I am trying to pass different functions which have pointers as arguments to a python function. One example of the input function as input parameter is the given normal function: Sample.pyxfrom cpython …