how to convert xml file to csv using python script

Question:

I have an xml file which have a special structure , I need to convert it to csv file using a script python
This is a part of my xml File :

<?xml version="1.0" encoding="UTF-8"?>
<results version="2">
    <cppcheck version="2.9"/>
    <errors>
        <error identifier="redundantAssignment" errorStyle="style" msg="Variable &apos;ret&apos; is reassigned a value before the old one has been used.">
            <location file="D:testmain.c" line="64" column="8" info="ret is overwritten"/>
            <location file="D:testmain.c" line="62" column="8" info="ret is assigned"/>
            <symbol>ret</symbol>
        </error>
        <error identifier="redundantAssignment" errorStyle="style" msg="Variable &apos;ret&apos; is reassigned a value before the old one has been used.">
            <location file="D:testdata.c" line="93" column="8" info="ret is overwritten"/>
            <location file="D:testdata.c" line="91" column="8" info="ret is assigned"/>
            <symbol>ret</symbol>
        </error>
    </errors>
</results>

I’m using this script but it doesn’t work for me :

import xml.etree.ElementTree as ET
import csv

# PARSE XML
xml = ET.parse("./error.xml")
root = xml.getElementsByTagName()

# CREATE CSV FILE
csvfile = open("data.csv",'w',encoding='utf-8')
csvfile_writer = csv.writer(csvfile)

# ADD THE HEADER TO CSV FILE
csvfile_writer.writerow(["identifier","file","errorStyle","msg"])

# FOR EACH EMPLOYEE
for error in root.findall("errors/error"):
    
    if(error):
       # EXTRACT EMPLOYEE DETAILS  
      identifier = error.get('identifier')
      file = error.find('file')
      errorStyle = error.find("errorStyle")
      msg = error.find("msg")
      csv_line = [identifier, file.text, errorStyle.text, msg.text]
      
      # ADD A NEW ROW TO CSV FILE
      csvfile_writer.writerow(csv_line)
csvfile.close()

Asked By: monaco Stephen

||

Answers:

Please refer to code below:

import xml.etree.ElementTree as ET
import csv

xml_data = """<?xml version="1.0" encoding="UTF-8"?>
<results version="2">
    <cppcheck version="2.9"/>
    <errors>
        <error identifier="redundantAssignment" errorStyle="style" msg="Variable &apos;ret&apos; is reassigned a value before the old one has been used.">
            <location file="Din.c" line="64" column="8" info="ret is overwritten"/>
            <location file="D.c" line="62" column="8" info="ret is assigned"/>
            <symbol>ret</symbol>
        </error>
        <error identifier="redundantAssignment" errorStyle="style" msg="Variable &apos;ret&apos; is reassigned a value before the old one has been used.">
            <location file="Dta.c" line="93" column="8" info="ret is overwritten"/>
            <location file="Dta.c" line="91" column="8" info="ret is assigned"/>
            <symbol>ret</symbol>
        </error>
    </errors>
</results>"""

root = ET.fromstring(xml_data)

csvfile = open("data.csv",'w')
csvfile_writer = csv.writer(csvfile)
csvfile_writer.writerow(["msg","identifier","errorStyle"])

for child in root:
    for item in child:
        csv_line = [item.attrib["msg"],item.attrib["identifier"] , item.attrib["errorStyle"]]
        csvfile_writer.writerow(csv_line)
        print item.attrib
csvfile.close()

Hope this helps, Thanks.

Answered By: mago
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.