BrechtDeMan@878: #!/usr/bin/python BrechtDeMan@880: # -*- coding: utf-8 -*- BrechtDeMan@878: nicholas@872: import xml.etree.ElementTree as ET nicholas@872: import os nicholas@872: import csv nicholas@872: BrechtDeMan@883: # XML results files location (modify as needed): BrechtDeMan@883: folder_name = "../saves" # Looks in 'saves/' folder from 'scripts/' folder BrechtDeMan@883: nicholas@872: # get every XML file in folder BrechtDeMan@883: for file in os.listdir(folder_name): nicholas@872: if file.endswith(".xml"): BrechtDeMan@883: tree = ET.parse(folder_name + '/' + file) nicholas@872: root = tree.getroot() nicholas@872: nicholas@872: # get list of all page names nicholas@872: for audioholder in root.findall("./audioholder"): # iterate over pages nicholas@872: page_name = audioholder.get('id') # get page name nicholas@872: nicholas@872: if page_name is None: # ignore 'empty' audio_holders nicholas@872: break nicholas@872: nicholas@872: # create folder [page_name] if not yet created BrechtDeMan@883: if not os.path.exists(folder_name + "/" + page_name): BrechtDeMan@883: os.makedirs(folder_name + "/" + page_name) nicholas@872: nicholas@872: # for page [page_name], print comments related to fragment [id] BrechtDeMan@883: for audioelement in audioholder.findall("./audioelement"): nicholas@872: if audioelement is not None: # Check it exists nicholas@872: audio_id = str(audioelement.get('id')) BrechtDeMan@878: BrechtDeMan@883: csv_name = folder_name +'/' + page_name+'/'+page_name+'-comments-'+audio_id+'.csv' nicholas@872: BrechtDeMan@878: # append (!) to file [page_name]/[page_name]-comments-[id].csv BrechtDeMan@878: with open(csv_name, 'a') as csvfile: BrechtDeMan@878: writer = csv.writer(csvfile, BrechtDeMan@878: delimiter=',', BrechtDeMan@878: dialect="excel", BrechtDeMan@878: quoting=csv.QUOTE_ALL) BrechtDeMan@883: commentstr = audioelement.find("./comment/response").text BrechtDeMan@883: nicholas@872: if commentstr is None: BrechtDeMan@883: commentstr = ''; BrechtDeMan@883: BrechtDeMan@883: # anonymous comments: BrechtDeMan@883: #writer.writerow([commentstr.encode("utf-8")]) BrechtDeMan@883: # comments with (file) name: BrechtDeMan@883: writer.writerow([file[:-4]] + [commentstr.encode("utf-8")]) nicholas@872: BrechtDeMan@878: #TODO Replace 'new line' in comment with something else? BrechtDeMan@878: BrechtDeMan@878: # PRO TIP: Change from csv to txt by running this in bash: BrechtDeMan@878: # $ cd folder_where_csvs_are/ BrechtDeMan@878: # $ for i in *.csv; do mv "$i" "${i/.csv}".txt; done