Присоединение к новой строке при конвертации из xml в csv в python - PullRequest
0 голосов
/ 26 марта 2020

У меня проблемы с записью каждого control_code вместе с description в новую строку, а не в одну и ту же строку, но в другой столбец (см. Изображение). Любые идеи будут оценены!

Это XML файл Я разбор

Мой Python Файл:

import xml.etree.ElementTree as ET 
import csv


xmlFile='/Users/userName/Desktop/xmlFile.xml'
tree = ET.parse(xmlFile) 
root = tree.getroot()

# open a file for writing
excelFile = open('/Users/userName/Desktop/csvTable.csv', 'w')

# creates the csv writer object / variable to write to csv
csvwriter = csv.writer(excelFile)
# list that contains the header
list_head = []
count = 0

for element in root.findall('control'):
    list_nodes=[]
    # address_list = []
    if count == 0:

        control_code='code'
        list_head.append(control_code)

        description = element.find('.//statement/description').tag
        list_head.append(description)

        csvwriter.writerow(list_head)
        count = count + 1

    # Control Description and Control Code Parsing 
    if element.find('statement'): 
        for controlStmt in element.findall('statement'):
            value1 = controlStmt.find('description').text
            if controlStmt.find('statement') is not None:
                for part2 in controlStmt.findall('statement'):
                    value2=part2.find('description').text
                    if part2.find('statement') is not None:
                        for part3 in part2.findall('statement'):
                            value3=part3.find('description').text
                            control_code=part3.find('number').text
                            list_nodes.append(control_code)
                            description=value1+value2+value3
                            list_nodes.append(description)

                    else:

                        value3=''
                        control_code=part2.find('number').text
                        list_nodes.append(control_code)
                        description=value1+value2
                        list_nodes.append(description)
            else:
                value2=''
                control_code=element.find('number').text
                list_nodes.append(control_code)
                description=value1
                list_nodes.append(description)
    else: 
        value1=''
    csvwriter.writerow(list_nodes)
excelFile.close()

Мой вывод enter image description here

1 Ответ

0 голосов
/ 26 марта 2020

Сброс list_nodes=[], когда вы хотите новую строку. Прямо сейчас у вас есть:

for element in root.findall('control'):
    list_nodes=[]
        ...
        for controlStmt in element.findall('statement'):
            ...
    csvwriter.writerow(list_nodes)

И оно должно быть:

for element in root.findall('control'):
        ...
        for controlStmt in element.findall('statement'):
            list_nodes=[]
            ...
            csvwriter.writerow(list_nodes)

Или, проще, пропустить список. Появляется:

for element in root.findall('control'):
        ...
        for controlStmt in element.findall('statement'):
            ...
            csvwriter.writerow([control_code,description])
...