comparison scripts/comment_parser.py @ 1030:057c6b039f4e

Comment and rating scripts: robust against audioholders without id or audioelements; fixes issue where plots were not cleared before adding new content; showing legend optional; robust against non-UTF8 characters in legend names
author Brecht De Man <BrechtDeMan@users.noreply.github.com>
date Mon, 15 Jun 2015 13:09:46 +0100
parents 4a0bfa7bef24
children ab79d76c6f85
comparison
equal deleted inserted replaced
1029:5a1313f44e93 1030:057c6b039f4e
9 root = tree.getroot() 9 root = tree.getroot()
10 10
11 # get list of all page names 11 # get list of all page names
12 for audioholder in root.findall("./audioholder"): # iterate over pages 12 for audioholder in root.findall("./audioholder"): # iterate over pages
13 page_name = audioholder.get('id') # get page name 13 page_name = audioholder.get('id') # get page name
14
15 if page_name is None: # ignore 'empty' audio_holders
16 break
14 17
15 # create folder [page_name] if not yet created 18 # create folder [page_name] if not yet created
16 if not os.path.exists(page_name): 19 if not os.path.exists(page_name):
17 os.makedirs(page_name) 20 os.makedirs(page_name)
18 21
19 # for page [page_name], print comments related to fragment [id] 22 # for page [page_name], print comments related to fragment [id]
20 for audioelement in root.findall("*/[@id='"+page_name+"']/audioelement"): 23 for audioelement in root.findall("*/[@id='"+page_name+"']/audioelement"):
21 audio_id = str(audioelement.get('id')) 24 if audioelement is not None: # Check it exists
22 # append to file [page_name]/[page_name]-comments-[id].csv 25 audio_id = str(audioelement.get('id'))
23 with open(page_name+'/'+page_name+'-comments-'+audio_id+'.csv', 'a') as csvfile:
24 commentstr = root.find("*/[@id='"
25 + page_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 26
35 #TODO Replace 'new line' with something else? 27 # append to file [page_name]/[page_name]-comments-[id].csv
28 with open(page_name+'/'+page_name+'-comments-'+audio_id+'.csv', 'a') as csvfile:
29 writer = csv.writer(csvfile, delimiter=',')
30 commentstr = root.find("*/[@id='"
31 + page_name
32 + "']/audioelement/[@id='"
33 + audio_id
34 + "']/comment/response").text
35 if commentstr is None:
36 writer.writerow([''])
37 else:
38 writer.writerow([commentstr.encode("utf-8")])
39 #TODO Comma doesn't act as delimiter now!
40 # (when adding more than just a comment per line):
41 # writer.writerow([file + ',' + commentstr.encode("utf-8")])
36 42
37 #TODO 'Append' means duplicate entries if run several times... 43 #TODO Replace 'new line' with something else?
38 44
45 #TODO 'Append' means duplicate entries if run several times...
46