'''
Created on Oct 28, 2013

@author: rcd
'''
import csv

def readCSVfile (filename, headerRows=0):
    # some OSes need to know that the file might have some special characters
    f = open(filename, 'rb')
    # convert reader to a list so we can close the file
    result = list(csv.reader(f, delimiter=',', quotechar='"'))
    # close the file so we do not take up extra system resources
    f.close()
    # throw away the header row(s) of the data
    return result[headerRows:]


def printCSVdata (data):
    """
    for debugging
    """
    for d in data:
        print(d)


def processCSVdata (data, result):
    for d in data:
        key = (d[1].strip(), d[2].strip())
        if key not in result:
            result[key] = 0
        result[key] += 1
    return result


# read data from files
top = readCSVfile("imdb_movie_toprated.txt")
gross = readCSVfile("imdb_movie_gross.txt")

# verify data was read properly
#printCSVdata(top)
#printCSVdata(gross)

# combine results of processing data multiple times
movies = {}
processCSVdata(top, movies)
processCSVdata(gross, movies)
print(len(movies))
print(movies)
