from collections import defaultdict import sys, getopt import re from operator import itemgetter # Read and process a weather information file. # It parse the codeSum column (#23) in the CSV file # and show the count of each codeSum. f = open(sys.argv[1], 'r') heading = f.readline().split(',') result = [line.strip().split(',') for line in f.readlines()] f.close() # count is a dictionary with the key being the individual CodeSummary count = defaultdict(int) for field in filter(lambda a: a, map(lambda w: w[22].strip(), result)): for sym in re.split('\s+', field): count[sym] += 1 # Print result in the sorted order of codeSum. for key, value in sorted(count.items(), key=itemgetter(0)): print (key + ": " + str(value))