annotate scripts/comment_parser.py @ 1513:f14d55481b60

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