-1

im at work and trying to make a script which adds only selected xml from to a new created xml file(from a csv). Here is what I got so far..Apologize for formatting

Create a script from a csv - works:

if len(sys.argv) != 2:
    os._exit(1)
path=sys.argv[1] # get folder as a command line argument
os.chdir(path)
csvFiles = [f for f in os.listdir('.') if f.endswith('.csv') or f.endswith('.CSV')]
for csvFile in csvFiles:
    xmlFile = csvFile[:-4] + '.xml'

etc.. (xml is created after my script from a csv..) Now I want to append from an xml in the same directory to the end of the newly created xml file..

Append xml into new script -failing!

xmlFiles = [x for x in os.listdir('.') if x.endswith('.xml') or f.endswith('.XML')]
for xmlFile in xmlFiles:

tree = ET.parse(xmlFile)
Meta = tree.getroot()

a = Meta.find('Speaker')
a1 = Meta.find('Device')

b = ET.SubElement(a, 'speaker')
c = ET.SubElement(b, 'device')
for i in range(len(tags)):
        tags[i] = tags[i].replace(' ', '_')
    else:
        xmlData.write('<row>' + "\n")
        for i in range(len(tags)):
            xmlData.write('    ' + '<' + tags[i] + '>' \
                          + row[i] + '</' + tags[i] + '>' + "\n")
        xmlData.write('</row>' + "\n")
    rowNum +=1
xmlData.write('</csv_data>' + "\n")
xmlData.close()

print ET.tostring(Meta)

Also if there is a better, different approach and refernce I would be happy :) Thanks

Community
  • 1
  • 1
Anekdotin
  • 1,531
  • 4
  • 21
  • 43
  • I just answered a [similar question](http://stackoverflow.com/questions/32651932/merging-lots-of-xml-files/32654614#32654614). After first XML is created, use XSLT to merge with other XML document using Python's lxml module, no need to manually re-create in code. – Parfait Sep 18 '15 at 17:22
  • You seem to be missing some code entering the outer for loop, presumably an `if` statement. – AChampion Sep 18 '15 at 18:03

1 Answers1

0

I ended up deleting that all, and using the lxml module. thanks guys

Anekdotin
  • 1,531
  • 4
  • 21
  • 43