如何使用python脚本将xml文件转换为csv

ou6hu8tu  于 2022-12-15  发布在  Python
关注(0)|答案(1)|浏览(147)

我有一个具有特殊结构的xml文件,我需要使用脚本Python将其转换为csv文件这是我的xml文件的一部分:

<?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:\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 identifier="redundantAssignment" errorStyle="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>

我正在使用此脚本,但它不适用于我:

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()
tcbh2hod

tcbh2hod1#

请参考以下代码:

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()

希望这能帮上忙,谢谢。

相关问题