BrechtDeMan@878
|
1 #!/usr/bin/python
|
BrechtDeMan@880
|
2 # -*- coding: utf-8 -*-
|
BrechtDeMan@878
|
3
|
nicholas@872
|
4 import xml.etree.ElementTree as ET
|
nicholas@872
|
5 import os
|
nicholas@872
|
6 import csv
|
nicholas@872
|
7
|
BrechtDeMan@883
|
8 # XML results files location (modify as needed):
|
BrechtDeMan@883
|
9 folder_name = "../saves" # Looks in 'saves/' folder from 'scripts/' folder
|
BrechtDeMan@883
|
10
|
nicholas@872
|
11 # get every XML file in folder
|
BrechtDeMan@883
|
12 for file in os.listdir(folder_name):
|
nicholas@872
|
13 if file.endswith(".xml"):
|
BrechtDeMan@883
|
14 tree = ET.parse(folder_name + '/' + file)
|
nicholas@872
|
15 root = tree.getroot()
|
nicholas@872
|
16
|
nicholas@872
|
17 # get list of all page names
|
nicholas@872
|
18 for audioholder in root.findall("./audioholder"): # iterate over pages
|
nicholas@872
|
19 page_name = audioholder.get('id') # get page name
|
nicholas@872
|
20
|
nicholas@872
|
21 if page_name is None: # ignore 'empty' audio_holders
|
nicholas@872
|
22 break
|
nicholas@872
|
23
|
nicholas@872
|
24 # create folder [page_name] if not yet created
|
BrechtDeMan@883
|
25 if not os.path.exists(folder_name + "/" + page_name):
|
BrechtDeMan@883
|
26 os.makedirs(folder_name + "/" + page_name)
|
nicholas@872
|
27
|
nicholas@872
|
28 # for page [page_name], print comments related to fragment [id]
|
BrechtDeMan@883
|
29 for audioelement in audioholder.findall("./audioelement"):
|
nicholas@872
|
30 if audioelement is not None: # Check it exists
|
nicholas@872
|
31 audio_id = str(audioelement.get('id'))
|
BrechtDeMan@878
|
32
|
BrechtDeMan@883
|
33 csv_name = folder_name +'/' + page_name+'/'+page_name+'-comments-'+audio_id+'.csv'
|
nicholas@872
|
34
|
BrechtDeMan@878
|
35 # append (!) to file [page_name]/[page_name]-comments-[id].csv
|
BrechtDeMan@878
|
36 with open(csv_name, 'a') as csvfile:
|
BrechtDeMan@878
|
37 writer = csv.writer(csvfile,
|
BrechtDeMan@878
|
38 delimiter=',',
|
BrechtDeMan@878
|
39 dialect="excel",
|
BrechtDeMan@878
|
40 quoting=csv.QUOTE_ALL)
|
BrechtDeMan@883
|
41 commentstr = audioelement.find("./comment/response").text
|
BrechtDeMan@883
|
42
|
nicholas@872
|
43 if commentstr is None:
|
BrechtDeMan@883
|
44 commentstr = '';
|
BrechtDeMan@883
|
45
|
BrechtDeMan@883
|
46 # anonymous comments:
|
BrechtDeMan@883
|
47 #writer.writerow([commentstr.encode("utf-8")])
|
BrechtDeMan@883
|
48 # comments with (file) name:
|
BrechtDeMan@883
|
49 writer.writerow([file[:-4]] + [commentstr.encode("utf-8")])
|
nicholas@872
|
50
|
BrechtDeMan@878
|
51 #TODO Replace 'new line' in comment with something else?
|
BrechtDeMan@878
|
52
|
BrechtDeMan@878
|
53 # PRO TIP: Change from csv to txt by running this in bash:
|
BrechtDeMan@878
|
54 # $ cd folder_where_csvs_are/
|
BrechtDeMan@878
|
55 # $ for i in *.csv; do mv "$i" "${i/.csv}".txt; done
|