Last active
May 2, 2019 16:01
-
-
Save mbstacy/c399d692888ffce3656614134143872b to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import xmltodict | |
import sys | |
def read_xml(filename): | |
with open(filename,'r') as f1: | |
return xmltodict.parse("<root>{0}</root>".format(f1.read()),cdata_key='text',attr_prefix='',dict_constructor=dict) | |
def search_subjects(term,doc): | |
total_hits=0 | |
total_records=0 | |
for record in doc['root']['result']: | |
total_records+=1 | |
record['fulltext']='' | |
if 'subjects' in record.keys(): | |
for itm in record['subjects']: | |
if term.lower() in itm.lower(): | |
total_hits+=1 | |
print ("{0}\n".format(record)) | |
break | |
print("Total Records: {0},Search Hits: {1}".format(total_records,total_hits)) | |
if __name__ == "__main__": | |
search_term=sys.argv[1] | |
filename=sys.argv[2] | |
search_subjects(search_term,read_xml(filename)) | |
# Installation | |
# pip install xmltodict | |
# Run Script | |
# python parseCongData.py <search term> <xml filename> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment