upload
By: a guest | Jun 19th, 2009 | Syntax:
Python | Size: 1.96 KB | Hits: 163 | Expires: Never
#!/usr/bin/python
#This script requires Python Version 2.5 or higher
#Define fields that should be selected
resultFields = (7, 8, 9, 12, 14, 15)
class ParseSGExport:
"Creates communication matrix from SG log export (csv file)"
def __init__(self):
"Define working list"
self.list = []
def unique(self):
"Removes duplicates from list"
uniqueList = []
for item in self.list:
if item not in uniqueList:
uniqueList.append(item)
self.list = uniqueList
def sort(self):
"Sorts list by colum 1, 2 and 4"
from operator import itemgetter
description = self.list.pop(0)
self.list.sort(key=itemgetter(0, 1, 3))
self.list.insert(0, description)
def parseFile(self, fileName, fieldTuple):
"Read CSV export file and parse it to a list"
import csv
try:
srcFile = csv.reader(open(fileName, 'r'), delimiter=',')
for row in srcFile:
resultRow = []
for i in fieldTuple:
resultRow.append(row[i])
self.list.append(resultRow)
except IOError:
print "IOError Exiting..."
exit()
def writeXLS(self, fileName):
"Write list to Excel compatible CSV file"
try:
import csv
dstFile = csv.writer(open(fileName, 'w'), dialect='excel')
for item in self.list:
dstFile.writerow(item)
except IOError:
print "IOError Exiting..."
exit()
if __name__ == "__main__":
import sys
if len(sys.argv) != 3:
print "Usage: python sgfilter.py <src file> <dst file>"
else:
sg = ParseSGExport()
sg.parseFile(sys.argv[1], resultFields)
sg.unique()
sg.sort()
sg.writeXLS(sys.argv[2])