Skip to content
Advertisement

XML file parsing with Python

I am having trouble parsing data in this manner for an XML file which is converted to a CSV: enter image description here

For the first column, I would like to get the general name tag (recordingSystem, Ports, etc) and concatenate it with the subNames in the row tags (closedFileCount, processedFileCount, etc)

The tag where the subName is located keeps changing, it could be a “usage”, “lwGuage”, “hwGauge” and so on. I also need to collect those and put it in the column beside it.

Please see the sample XML below:

JavaScript

The code below is supposed to get the GeneralName and display it in the csv file the correct number of times but I can not get it to display anything.

JavaScript

Advertisement

Answer

If xml_string is your XML snippet from the question, then this script:

JavaScript

Produces data.csv (screenshot from LibreOffice):

enter image description here


Edit: To get measures tag into a column, you can do:

JavaScript

Produces:

enter image description here

User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement