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