BrechtDeMan@927: import xml.etree.ElementTree as ET BrechtDeMan@927: import os BrechtDeMan@927: import csv BrechtDeMan@927: BrechtDeMan@927: # get every XML file in folder BrechtDeMan@927: for file in os.listdir("."): # You have to put this script in folder where output XML files are. BrechtDeMan@927: if file.endswith(".xml"): BrechtDeMan@927: tree = ET.parse(file) BrechtDeMan@927: root = tree.getroot() BrechtDeMan@927: BrechtDeMan@927: # get list of all songs BrechtDeMan@927: for audioholder in root.findall("./audioholder"): # iterate over songs BrechtDeMan@927: song_name = audioholder.get('id') # get song name BrechtDeMan@927: BrechtDeMan@927: # create folder [song_name] if not yet created BrechtDeMan@927: if not os.path.exists(song_name): BrechtDeMan@927: os.makedirs(song_name) BrechtDeMan@927: BrechtDeMan@927: # for song [song_name], print comments related to mix [id] BrechtDeMan@927: for audioelement in root.findall("*/[@id='"+song_name+"']/audioelement"): BrechtDeMan@927: audio_id = str(audioelement.get('id')) BrechtDeMan@927: # append to file [song_name]/[song_name]-comments-[id].csv BrechtDeMan@927: with open(song_name+'/'+song_name+'-comments-'+audio_id+'.csv', 'a') as csvfile: BrechtDeMan@927: commentstr = root.find("*/[@id='" BrechtDeMan@927: + song_name BrechtDeMan@927: + "']/audioelement/[@id='" BrechtDeMan@927: + audio_id BrechtDeMan@927: + "']/comment/response").text BrechtDeMan@927: writer = csv.writer(csvfile, delimiter=',') BrechtDeMan@927: writer.writerow([commentstr.encode("utf-8")]) BrechtDeMan@927: #TODO Comma doesn't act as delimiter now! BrechtDeMan@927: # (when adding more than just a comment per line): BrechtDeMan@927: # writer.writerow([file + ',' + commentstr.encode("utf-8")]) BrechtDeMan@927: BrechtDeMan@927: #TODO Replace 'new line' with something else? BrechtDeMan@927: BrechtDeMan@927: #TODO 'Append' means duplicate entries if run several times...