xml - How to iterate over time interval in a two-level dict in Python? -
i'm creating program read set of xml files in directory , prints summary report of content, so:
timestamp tick12 tick13 tick14 ------------ ------ ------ ------ 06-26 09:00 1 0 0 06-26 09:01 0 33 0 06-26 09:02 0 0 32 06-26 09:03 0 19 0 06-26 09:04 0 12 0
each column has timestamp (by minute) followed 1 column per print zone found, , each line shows timestamp , number of requests each print zone during time period. each minute should accounted for, if 0 requests made, , program should accept zone names cl zones reported.
now, i'm having difficulty traversing timestamps inside dict() such output includes not timestamps pulled dir
of xml files, timestamps in between (minute-by-minute). timestamps in xml files formatted so:
2017-06-22t06:34:21
any advice on design welcome, i'm new python!
#!/usr/bin/python import os, sys xml.dom.minidom import parse import xml.dom.minidom datetime import datetime, timedelta import glob import time global fpath1 fpath1 = r'/users/xd/documents/pz/testdir' os.chdir(fpath1) timeheader = 'timestamp' myarg = sysargv[1] def main(): print('%11s %20s' % (timeheader, myarg)) print('-----------\t\t-------') parseandformat() format_old = '%y-%m-%dt%h:%m:%s' format_new = '%%m-%d %h:%m' def parseandformat(): fn in glob.glob(os.path.join(fpath1, '*.xml')): domtree = xml.dom.minidom.parse(fn) collection = domtree.documentelement ts = collection.getelementbytagname('timestamp')[0].firstchild.data pz = collection.getelementbytagname('printzone')[0].firstchild.data ts = datetime.strptime(timestamp, format_old).strftime(format_new) # ?? d[ts] = d.get(ts, {}) d[ts][pz] = 1 + d[ts].get(pz, 0) keys = d.keys() mintime = min(keys) maxtime = max(keys) k in keys: print '%11 %20s' % (k, d[k].get(myarg, 0)) result in perdelta(mintime, maxtime, timedelta(seconds=60)): print result def perdelta(start, end, delta): curr = start while curr < end: yield curr curr += delta main()
Comments
Post a Comment