0

I have an xml file which was converted to a CSV file using python script. The conversion was made successfully but just the last row of the CSV file was generated. I need a CSV file with all the data present in the xml file.

This is the xml file that I'm using for the conversion: `

<?xml version="1.0" encoding="UTF-8"?>
    <errors>
        <error id="redundantAssignment" severity="style" msg="Variable &apos;ret&apos; is reassigned a value before the old one has been used.">
            <location file="D:\test\main.c" line="64" column="8" info="ret is overwritten"/>
            <location file="D:\test\main.c" line="62" column="8" info="ret is assigned"/>
            <symbol>ret</symbol>
        </error>
        <error id="redundantAssignment" severity="style" msg="Variable &apos;ret&apos; is reassigned a value before the old one has been used.">
            <location file="D:\test\data.c" line="93" column="8" info="ret is overwritten"/>
            <location file="D:\test\data.c" line="91" column="8" info="ret is assigned"/>
            <symbol>ret</symbol>
        </error>
    </errors>
</results>

`

This is the script python that I'm runing right now :

import xml.etree.ElementTree as ET
import csv

xml_data = open('error.xml', 'r').read()

root = ET.fromstring(xml_data)

csvfile = open("data.csv",'w', newline='')
csvfile_writer = csv.writer(csvfile)
csvfile_writer.writerow(["id","severity","msg","file","line"])

for child in root.findall("errors"):
    for item in child:
        csv_line1 = [item.attrib["id"],item.attrib["severity"] , item.attrib["msg"]]
        print(item.attrib)
        #print("heeere1")
        for location in root.iter('location'):
            csv_line2 = [location.attrib["file"],location.attrib["line"]]
            #print("heeere2")
            print(location.attrib)
    csvfile_writer.writerow(csv_line1 + csv_line2)
    
csvfile.close()

And this is the output of the script : enter image description here

1 Answers1

0

if you are trying to generate a file like this one enter image description here

just change the indentation of your code and put csvfile_writer.writerow(csv_line1 + csv_line2) inside this loop for location in root.iter('location')

yasmine
  • 434
  • 4
  • 6
  • it works fine , but it shows 8 rows since the xml contains 4 rows only I think there is a duplication – monaco Stephen Dec 13 '22 at 15:27
  • change this `for location in root.iter('location')` to this `for location in item.findall('location')` I think you are looping errors and instead of looping locations inside errors, you are looping all the locations – yasmine Dec 13 '22 at 15:40
  • It works fine now. Thanks for your help. The output was displayed in CSV format rather than the screenshot you included with your response, therefore Is there any modification on the script to be generated in XL format ? – monaco Stephen Dec 13 '22 at 15:47
  • That is only because we are using different ways to open the file, but there are ways where you can save your data to an excel file instead of a csv file. check this https://stackoverflow.com/questions/17684610/python-convert-csv-to-xlsx – yasmine Dec 13 '22 at 15:59
  • It works very well with this example. Thanks for your effort! – monaco Stephen Dec 14 '22 at 10:59
  • glad I helped ^^ – yasmine Dec 14 '22 at 13:00