#!/usr/bin/env python # encoding: utf-8 """ truncateTalosRSS.py Created by Rob Campbell on 2008-09-08. Copyright (c) 2008 AntennaSoft Inc.. All rights reserved. """ import sys import getopt help_message = ''' Truncate a given Tp_RSS comma-separated-values file down to a smaller size ''' class Usage(Exception): def __init__(self, msg): self.msg = msg def readFile(path): infile = open(path, "r") lines = infile.readlines() infile.close() contents = [] for each in lines: contents.append(each.strip().strip()) return contents def parseFile(lines): bun = {} for each in lines: (i, value) = each.split(',') if i != 'i': bun[int(i)] = value return bun def writeFile(outFile, d): file = open(outFile, "w") for (k, v) in d.iteritems(): file.write(str(k) + "," + str(v) + "\n") file.close() def main(argv=None): if argv is None: argv = sys.argv try: try: opts, args = getopt.getopt(argv[1:], "hi:o:n:", ["help", "input=", "output=", "number="]) except getopt.error, msg: raise Usage(msg) # option processing for option, value in opts: if option in ("-h", "--help"): raise Usage(help_message) if option in ("-o", "--output"): output = value if option in ("-i", "--input"): infile = value if option in ("-n", "--number"): number = int(value) contents = readFile(infile) contentDict = parseFile(contents) shortbun = {} divisor = len(contentDict) // number for i in xrange(number + 1): chunk = i * divisor a = 0 for j in xrange(divisor): a += int(contentDict[chunk + j]) shortbun[i] = a // divisor writeFile(output, shortbun) except Usage, err: print >> sys.stderr, sys.argv[0].split("/")[-1] + ": " + str(err.msg) print >> sys.stderr, "\t for help use --help" return 2 if __name__ == '__main__': main()