Created
December 21, 2012 16:14
-
-
Save alexstorer/4353760 to your computer and use it in GitHub Desktop.
Parse some XML
This file contains hidden or 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
| from lxml import etree | |
| import re | |
| import csv | |
| import os.path as op | |
| import sys | |
| import glob | |
| class NLRPParse(object): | |
| def __init__(self,fname): | |
| prefix = op.splitext(op.basename(fname))[0] | |
| loc = op.split(fname)[0] | |
| f = open(fname,'r') | |
| tree = etree.parse(f) | |
| fw = open(op.join(loc,prefix+'.csv'),'w') | |
| allcolumns = [] | |
| allrows = tree.xpath('//row') | |
| # just get the column names from the first row | |
| # ASSUMES THEY ARE THE SAME FOR EACH ROW! | |
| for col in allrows[0]: | |
| allcolumns.append(col.tag) | |
| self.dw = csv.DictWriter(fw,allcolumns) | |
| self.dw.writeheader() | |
| allrows = tree.xpath('//row') | |
| for r in allrows: | |
| d = dict() | |
| for col in r: | |
| if col.text is not None: | |
| bettertext = re.sub('\r','',col.text) | |
| bettertext = re.sub('\n','',bettertext) | |
| d[col.tag] = bettertext.strip().encode('utf-8') | |
| else: | |
| d[col.tag] = "" | |
| self.dw.writerow(d) | |
| f.close() | |
| fw.close() | |
| for a in glob.glob(sys.argv[1]): | |
| suffix = op.splitext(op.basename(a))[1] | |
| if suffix=='.xml': | |
| print "Parsing file: ", a | |
| p = NLRPParse(a) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment