comparison scripts/comment_parser.py @ 927:5db0069046d5

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