1

This program basically flattens an xml file and writes it to csv.

My issue is that the ‘row’ variable in ‘values_loop’ isn’t resetting between calls. Every time I call it the new values are appended to the old ones.

Basically I’m getting this:

A  B  C
A  B  C  D  E  F
A  B  C  D  E  F  G  H  I

When I should get this

A  B  C
D  E  F
G  H  I

My code:

import csv
import xml.etree.ElementTree as ET

file_name = '2.xml'
root = ET.ElementTree(file=file_name).getroot() 
csv_file_name = '.'.join(file_name.split('.')[:-1]) + ".txt"

print csv_file_name

with open(csv_file_name, 'w') as file_:
    writer = csv.writer(file_, delimiter="\t")

    def header_loop(root, i=0, row=[]):
        for child in root:
            #print "\t"*i, child.tag.replace("{http://www.tes.com/aps/response}",""), child.attrib, i
            row.extend([child.tag.replace("{http://www.tes.com/aps/response}","")])
            header_loop(child,i+1)
        if i==0: return row

    def values_loop(root, i=0, row=[]):
        for child in root:
            #print "\t"*i, child.tag.replace("{http://www.tes.com/aps/response}",""), child.attrib, i
            row.extend([child.text])
            #print child.text
            values_loop(child,i+1)
        if i==0: return row


    #write the header 
    writer.writerow(header_loop(root[3]))

    #write the values
    writer.writerow(values_loop(root[3]))
    writer.writerow(values_loop(root[4]))
Bryan Oakley
  • 370,779
  • 53
  • 539
  • 685
Aman Chawla
  • 704
  • 2
  • 8
  • 25

1 Answers1

1

Set None as the default value for row.

def values_loop(root, i=0, row=None):
    if row == None:
        row = []
timgeb
  • 76,762
  • 20
  • 123
  • 145