annotate scripts/comment_parser.py @ 146:2d08d2025258

Comment parsing: automatically detect which songs - no need to manually enter song IDs
author Brecht De Man <b.deman@qmul.ac.uk>
date Sat, 30 May 2015 18:12:32 +0100
parents
children 927d05a43a70
rev   line source
b@146 1 import xml.etree.ElementTree as ET
b@146 2 import os
b@146 3 import csv
b@146 4
b@146 5 # get every XML file in folder
b@146 6 for file in os.listdir("."): # You have to put this script in folder where output XML files are.
b@146 7 if file.endswith(".xml"):
b@146 8 tree = ET.parse(file)
b@146 9 root = tree.getroot()
b@146 10
b@146 11 # get list of all songs
b@146 12 for audioholder in root.findall("./audioholder"): # iterate over songs
b@146 13 song_name = audioholder.get('id') # get song name
b@146 14
b@146 15 # create folder [song_name] if not yet created
b@146 16 if not os.path.exists(song_name):
b@146 17 os.makedirs(song_name)
b@146 18
b@146 19 # for song [song_name], print comments related to mix [id]
b@146 20 for audioelement in root.findall("*/[@id='"+song_name+"']/audioelement"):
b@146 21 audio_id = str(audioelement.get('id'))
b@146 22 # append to file [song_name]/[song_name]-comments-[id].csv
b@146 23 with open(song_name+'/'+song_name+'-comments-'+audio_id+'.csv', 'a') as csvfile:
b@146 24 commentstr = root.find("*/[@id='"
b@146 25 + song_name
b@146 26 + "']/audioelement/[@id='"
b@146 27 + audio_id
b@146 28 + "']/comment/response").text
b@146 29 writer = csv.writer(csvfile, delimiter=',')
b@146 30 writer.writerow([commentstr.encode("utf-8")])
b@146 31 #TODO Comma doesn't act as delimiter now!
b@146 32 # (when adding more than just a comment per line):
b@146 33 # writer.writerow([file + ',' + commentstr.encode("utf-8")])
b@146 34
b@146 35 #TODO Replace 'new line' with something else?
b@146 36
b@146 37 #TODO 'Append' means duplicate entries if run several times...