Code takes a directory where multiple XML files are located and parses them into a csv file.

from xml.etree import ElementTree as ET
from collections import defaultdict
import csv
from pathlib import Path

directory = 'C:/Users/z647818/Desktop/text'

with open('newfile2.csv', 'w', newline='') as f:
    writer = csv.writer(f)

    headers = ['id', 'service_code', 'rational', 'qualify', 'description_num', 'description_txt', 'set_data_xin', 'set_data_xax', 'set_data_value', 'set_data_x','identify']

    xml_files_list = list(map(str, Path(directory).glob('**/*.xml')))
    for xml_file in xml_files_list:
        tree = ET.parse(xml_file)
        root = tree.getroot()
        p_get = tree.find('.//Phones/Get').text
        p_set = tree.find('.//Phones/Set').text

        start_nodes = root.findall('.//START')
        for sn in start_nodes:
            row = defaultdict(str)

            repeated_values = dict()
            for k,v in sn.attrib.items():
                repeated_values[k] = v

            for rn in sn.findall('.//Rational'):
                repeated_values['rational'] = rn.text

            for qu in sn.findall('.//Qualify'):
                repeated_values['qualify'] = qu.text

            for ds in sn.findall('.//Description'):
                repeated_values['description_txt'] = ds.text
                repeated_values['description_num'] = ds.attrib['num']

            for st in sn.findall('.//SetData'):
                for k,v in st.attrib.items():
                    row['set_data_'+ str(k)] = v
                for key in repeated_values.keys():
                    row[key] = repeated_values[key]
                row_data = [row[i] for i in headers]
                row_data.insert(10, p_get + '_' + p_set)
                row = defaultdict(str)

This is how the output looks like.

enter image description here

Checked the terms and conditions and saw that this fall into “Correctness in unanticipated cases”

I wanted to just rearrange on how the csv file looks so I moved Identify at the beginning of the header. The lines below are related to Identify header. I just changed the index to Zero so this header is in first column.

    p_get = tree.find('.//Phones/Get').text
    p_set = tree.find('.//Phones/Set').text

here index from 10 to 0

row_data.insert(0, p_get + '_' + p_set)

The output of the files now looks like this. The other columns are shifted by one.

enter image description here

XML file

<?xml version="1.0" encoding="utf-8"?>
    <Date />
    <Prog />
    <Box />
    <Feature />
    <Nr />
    <START id="B001" service_code="0x5196">
      <Docs Docs_type="START">
      <Description num="1213f2312">The parameter</Description>
      <DataFile dg="12" dg_id="let">
        <SetData value="32" />
    <START id="C003" service_code="0x517B">
      <Docs Docs_type="START">
      <Description num="3423423f3423">The third</Description>
      <DataFile dg="55" dg_id="big">
        <SetData x="E1" value="21259" />
        <SetData x="E2" value="02" />
    <START id="Z048" service_code="0x5198">
      <RawData rawdata_type="ASDS">
      <Description num="434234234">The forth</Description>
      <DataFile unit="21" unit_id="FEDS">
        <XAxis unit="eg" discrete="false" axis_pts="19" name="Vsome" text_id="bx5" unit_id="GDFSD" />
        <SetData xin="5" xax="233" value="323" />
        <SetData xin="123" xax="77" value="555" />
        <SetData xin="17" xax="65" value="23" />
  • \$\begingroup\$ for rn in sn.findall('.//Rational'): repeated_values['rational'] = rn.text - why overwriting the same key value on each next iteration? That makes suspicion that the program may not work as expected. \$\endgroup\$ – RomanPerekhrest Jan 14 at 14:52
  • \$\begingroup\$ @RomanPerekhrest part of how the output should look like becuase sometimes set_data_x might have multiple rows so the others elements should just be repeated \$\endgroup\$ – ebe Jan 14 at 14:54
  • \$\begingroup\$ @RomanPerekhrest , any help regarding this question. If necessary I can upload the xml file and a screenshot of the output to show why some elements are repeated \$\endgroup\$ – ebe Jan 14 at 19:31
  • \$\begingroup\$ Yes, please share the link to uploadable xml file and post the final expected output as text (not image) \$\endgroup\$ – RomanPerekhrest Jan 14 at 20:08
  • \$\begingroup\$ @RomanPerekhrest , I have added the xml file. and this is the csv file that is created when row_data.insert(0, p_get + '_' + p_set) and identify is at the start of the header. Link to the file gofile.io/?c=9atylD \$\endgroup\$ – ebe Jan 14 at 20:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.