import sys, getopt import re from operator import itemgetter # getopt: C-style parser for command line options. # sys: System-specific parameters and functions. # re: regular expression operation # operator: methods for built-in operators. # (useful when passing the function as a parameter. # 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') result = []; heading = f.readline().split(',') num_line = 0; for line in f: line = line.rstrip() # strip trailing white spaces # result is an array of arrays: # Add the array contains columns of the current line # to result. result.append(line.split(',')) num_line = num_line + 1 f.close() # Debug: # for i in range(len(result)): # print (str(i) + ":" + str(result[i][22])) # count is a dictionary with the key being the individual CodeSummary count = {}; for i in range(len(result)): # process one reading. line = result[i][22].strip() if line: # Get all codeSummary and update their counts. summary = re.split('\s+', line) for j in range(len(summary)): # Debug: # print (str(j) + ":" + str(summary[j]) + "---") if summary[j] in count.keys(): count[summary[j]] += 1 else: count[summary[j]] = 1 # Print result inthe sorted order of codeSum. for key, value in sorted(count.items(), key=itemgetter(0)): print (key + ": " + str(value))