borInput = borInput.title() boroughZipRef = open('boroughs.csv','r') zipPopRef = open('zipCodes.csv','r') #Set the input borough as an instance of the class Borough borObj = Borough(borInput) #Add to the list of zip codes in the borough class using the method addZipcode boroughDict = csv.DictReader(boroughZipRef, ['ZipVal','BorName']) for row in boroughDict: if row['BorName'] == borInput: borObj.addZipcode(row['ZipVal']) #Sum the population in the zips of the borough by comparing zipcode values from the file to the zipcodes list in boroughs popBorInput = csv.DictReader(zipPopRef) totPopBor = 0 count = 0 for row in popBorInput: if row['Total Population per ZIP Code'] != '': pop = row['Total Population per ZIP Code'] zipcode = row['name'] if zipcode in borObj.zipcodes: count = count + 1 totPopBor = totPopBor + int(pop) #Calculate Average through the method in the borough class borObj.calcAvgPop(totPopBor, count) print borObj.avg
if line[1] == 'Bronx': countBx = countBx +1 if line[1] == 'Brooklyn': countBrook = countBrook +1 if line[1] == 'Staten': countSI = countSI +1 if line[1] == 'Queens': countQ = countQ +1 print countM print countBx print countBrook print countSI print countQ #use the class borough to calculate the average population for the given borough BorObj.calcAvgPop(totPop, count) #I tried to do it this way by making a dictionary and appending the population #to the zip and borough, but it kept giving me errors. And then I found a different #way to do it with collections but still gave me errors. # boroughgroup = collections.defaultdict(list) # boroughgroup = {} # for line in csvreaderb: # if line[0]>10000: # line[1] = line[1] # count = 0 # for line in csvreaderzc: # if (count>0 and line[0] in boroughgroup):