annotate scripts/comment_parser.py @ 1453:04e8a9c07c7e

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