import sys, getopt import re # imported three packages """ Hello, junk here """ "hello" 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. # sys.argv[0]: codeSum.py # sys.argv[1]: 201401daily_sample.csv # 'r': mode, read-only f = open(sys.argv[1], 'r') result = []; # an empty list. heading = f.readline().split(',') # print(heading) num_line = 0; for line in f: # file contains an iterator # print(line) 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() print (result) # 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))