comparison scripts/comment_parser.py @ 878:302926cdf3c4

Scripts: comment extraction bug fixes
author Brecht De Man <BrechtDeMan@users.noreply.github.com>
date Fri, 19 Jun 2015 16:24:08 +0100
parents 11e6243fafa9
children 24d0d3111c00
comparison
equal deleted inserted replaced
877:b1c05278f8fe 878:302926cdf3c4
1 #!/usr/bin/python
2
1 import xml.etree.ElementTree as ET 3 import xml.etree.ElementTree as ET
2 import os 4 import os
3 import csv 5 import csv
4 6
5 # get every XML file in folder 7 # get every XML file in folder
21 23
22 # for page [page_name], print comments related to fragment [id] 24 # for page [page_name], print comments related to fragment [id]
23 for audioelement in root.findall("*/[@id='"+page_name+"']/audioelement"): 25 for audioelement in root.findall("*/[@id='"+page_name+"']/audioelement"):
24 if audioelement is not None: # Check it exists 26 if audioelement is not None: # Check it exists
25 audio_id = str(audioelement.get('id')) 27 audio_id = str(audioelement.get('id'))
28
29
30 csv_name = page_name+'/'+page_name+'-comments-'+audio_id+'.csv'
26 31
27 # append to file [page_name]/[page_name]-comments-[id].csv 32 # append (!) to file [page_name]/[page_name]-comments-[id].csv
28 with open(page_name+'/'+page_name+'-comments-'+audio_id+'.csv', 'a') as csvfile: 33 with open(csv_name, 'a') as csvfile:
29 writer = csv.writer(csvfile, delimiter=',') 34 writer = csv.writer(csvfile,
35 delimiter=',',
36 dialect="excel",
37 quoting=csv.QUOTE_ALL)
30 commentstr = root.find("*/[@id='" 38 commentstr = root.find("*/[@id='"
31 + page_name 39 + page_name
32 + "']/audioelement/[@id='" 40 + "']/audioelement/[@id='"
33 + audio_id 41 + audio_id
34 + "']/comment/response").text 42 + "']/comment/response").text
35 if commentstr is None: 43 if commentstr is None:
36 writer.writerow(['']) 44 writer.writerow([''])
37 else: 45 else:
38 writer.writerow([commentstr.encode("utf-8")]) 46 # anonymous comments:
39 #TODO Comma doesn't act as delimiter now! 47 writer.writerow([commentstr])
40 # (when adding more than just a comment per line): 48 # comments with (file) name:
41 # writer.writerow([file + ',' + commentstr.encode("utf-8")]) 49 #writer.writerow([file[:-4]] + [commentstr])
42 50
43 #TODO Replace 'new line' with something else? 51 #TODO Replace 'new line' in comment with something else?
44 52
45 #TODO 'Append' means duplicate entries if run several times... 53 # PRO TIP: Change from csv to txt by running this in bash:
46 54 # $ cd folder_where_csvs_are/
55 # $ for i in *.csv; do mv "$i" "${i/.csv}".txt; done