-2

I need a python script for extract data from xml file

I have a xml file as shoen below:

<software>
    <name>Update Image</name>
    <Build>22.02</Build>
    <description>Firmware for Delta-M Series </description>
    <CommonImages>  </CommonImages>
<ModelBasedImages>
    <ULT>
        <CNTRL_0>
             <file type="UI_APP" ver="2.35" crc="1234"/>
             <file type="MainFW" ver="5.01" crc="5678"/>
             <SIZE300>
                <file type="ParamTableDB" ver="1.1.4" crc="9101"/>
             </SIZE300>
        </CNTRL_0>
        <CNTRL_2>
            <file type="UI_APP" ver="2.35" crc="1234"/>
            <file type="MainFW" ver="5.01" crc="9158"/>
        </CNTRL_2>  
    </ULT>
</ModelBasedImages>
</software>

I want the data in table format like:

type ver crc
UI_APP 2.35 1234
MainFW 5.01 5678
ParamTableDB 1.1.4 9101
UI_APP 2.35 1234
MainFW 5.01 9158

Extract into any type of file csv/doc....

I tried this code:

import xml.etree.ElementTree as ET
import csv

tree = ET.parse("Build_40.01 (copy).xml")
root = tree.getroot()

# open a file for writing

Resident_data = open('ResidentData.csv', 'w')

# create the csv writer object

csvwriter = csv.writer(Resident_data)
resident_head = []

count = 0
for member in root.findall('file'):
    resident = []
    address_list = []
    if count == 0:
        name = member.find('type').tag
        resident_head.append(name)
        ver = member.find('ver').tag
        resident_head.append(ver)
        crc = member.find('crc').tag
        resident_head.append(crc)

        csvwriter.writerow(resident_head)
        count = count + 1

    name = member.find('type').text
    resident.append(name)
    ver = member.find('ver').text
    resident.append(ver)
    crc = member.find('crc').text
    resident.append(crc)

    csvwriter.writerow(resident)
Resident_data.close()

Thanks in advance edited:xml code updated.

Sanjeev Kumar
  • 13
  • 1
  • 4

1 Answers1

1

Use the xpath expression .//file to find all <file> elements in the XML document, and then use each element's attributes to populate the CSV file through a csv.DictWriter:

import csv
import xml.etree.ElementTree as ET

tree = ET.parse("Build_40.01 (copy).xml")
root = tree.getroot()

with open('ResidentData.csv', 'w') as f:
    w = csv.DictWriter(f, fieldnames=('type', 'ver', 'crc'))
    w.writerheader()
    w.writerows(e.attrib for e in root.findall('.//file'))

For your sample input the output CSV file will look like this:

type,ver,crc
UI_APP,2.35,1234
MainFW,5.01,5678
ParamTableDB,1.1.4,9101
UI_APP,2.35,1234
MainFW,5.01,9158

which uses the default delimiter (comma) for a CSV file. You can change the delimiter using the delimiter=' ' option to DictWriter(), however, you will not be able to obtain the same formatting as your sample output, which appears to use fixed width fields (but you might get away with using tab as the delimiter).

mhawke
  • 84,695
  • 9
  • 117
  • 138