# open the db file containing zipcodes for each borough dbFile = open("boroughs.csv", "r") # create a Borough object borough = Borough(userBorough) # look through db file for zipcodes in that borough for line in dbFile: tempArray = line.strip().split(",") zipnum = tempArray[0] # if its in the borough add it to the object if lower(tempArray[1]) == userBorough: borough.addZipcode(zipnum) dbFile.close() # open the next file and pull relevant pop and area information into borough object dbFile = open("zipCodes.csv", "r") for line in dbFile: tempArray = line.strip().split(",") zipnum = tempArray[1] # check that this is a zipcode in the user supplied borough if zipnum in borough.zipcodes: # check that all values exist for that zip # check that area is valid if len(tempArray[7]) > 0: area = float(tempArray[7])
import sys import csv from borough import Borough borInput = sys.argv[1] 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)
for i in range(1, num_linesBorough): if linesBorough[i].split(',')[10] != '\n': zipBoroughDict[linesBorough[i].split(',')[0] = (float(linesBorough[i].split(',')[1][:-1])) filePopulation = open('zipCodes_tr.csv', 'r') linesPopulation = [] for line in filePopulation: linesPopulation.append(line) filePopulation.close() zipPopulationDict = {} # create dictionary as in problem3.py num_linesPopulation = len(linesPopulation) for i in range(1, num_linesPopulation): if linesPopulation[i].split(',')[10] != '\n': zipPopulationDict[linesPopulation[i].split(',')[0] = (float(linesPopulation[i].split(',')[10])) # Last part for zip in zipBoroughDict: if zipBoroughDict[zip][0:5] == targetBorough[0:5]: if zip in zipPopulationDict: zipCode = zipcode.Zipcode(zip, zipPopulationDict[zip]) targetBorough.addZipcode(zipCode) # Output average population average = targetBorough.averagePopulation() print average
import sys from borough import Borough from zipcode import Zipcode boroughOfInterest = Borough(sys.argv[1].split(" ")[0]) #load the zipcodes for the boroughs #load the population for the boroughs boroughPopDict = {'Bronx': 2764960, 'Brooklyn': 5009400, 'Staten': 937460, 'Manhattan': 3037768, 'Queens': 4459862} #add population to the borough object boroughOfInterest.population = boroughPopDict[boroughOfInterest.name] #add zipcodes to borough object boroughZipDict = {'Bronx': ['10451', '10452', '10453', '10454', '10455', '10456', '10457', '10458', '10459', '10460', '10461', '10462', '10463', '10464', '10465', '10466', '10467', '10468', '10469', '10470', '10471', '10472', '10473', '10474', '10475', '10451', '10452', '10453', '10454', '10455', '10456', '10457', '10458', '10459', '10460', '10461', '10462', '10463', '10464', '10465', '10466', '10467', '10468', '10469', '10470', '10471', '10472', '10473', '10474', '10475'], 'Brooklyn': ['11201', '11203', '11204', '11205', '11206', '11207', '11208', '11209', '11210', '11211', '11212', '11213', '11214', '11215', '11216', '11217', '11218', '11219', '11220', '11221', '11222', '11223', '11224', '11225', '11226', '11228', '11229', '11230', '11231', '11232', '11233', '11234', '11235', '11236', '11237', '11238', '11239', '11241', '11242', '11243', '11249', '11252', '11256', '11201', '11203', '11204', '11205', '11206', '11207', '11208', '11209', '11210', '11211', '11212', '11213', '11214', '11215', '11216', '11217', '11218', '11219', '11220', '11221', '11222', '11223', '11224', '11225', '11226', '11228', '11229', '11230', '11231', '11232', '11233', '11234', '11235', '11236', '11237', '11238', '11239', '11241', '11242', '11243', '11249', '11252', '11256'], 'Queens': ['11004', '11101', '11102', '11103', '11104', '11105', '11106', '11109', '11351', '11354', '11355', '11356', '11357', '11358', '11359', '11360', '11361', '11362', '11363', '11364', '11365', '11366', '11367', '11368', '11369', '11370', '11371', '11372', '11373', '11374', '11375', '11377', '11378', '11379', '11385', '11411', '11412', '11413', '11414', '11415', '11416', '11417', '11418', '11419', '11420', '11421', '11422', '11423', '11426', '11427', '11428', '11429', '11430', '11432', '11433', '11434', '11435', '11436', '11691', '11692', '11693', '11694', '11697', '11101', '11102', '11103', '11004', '11104', '11105', '11106', '11109', '11351', '11354', '11355', '11356', '11357', '11358', '11359', '11360', '11361', '11362', '11363', '11364', '11365', '11366', '11367', '11368', '11369', '11370', '11371', '11372', '11373', '11374', '11375', '11377', '11378', '11379', '11385', '11411', '11412', '11413', '11414', '11415', '11416', '11417', '11418', '11419', '11420', '11421', '11422', '11423', '11426', '11427', '11428', '11429', '11430', '11432', '11433', '11434', '11435', '11436', '11691', '11692', '11693', '11694', '11697'], 'Manhattan': ['10001', '10002', '10003', '10004', '10005', '10006', '10007', '10009', '10010', '10011', '10012', '10013', '10014', '10015', '10016', '10017', '10018', '10019', '10020', '10021', '10022', '10023', '10024', '10025', '10026', '10027', '10028', '10029', '10030', '10031', '10032', '10033', '10034', '10035', '10036', '10037', '10038', '10039', '10040', '10041', '10044', '10045', '10048', '10055', '10060', '10069', '10090', '10095', '10098', '10099', '10103', '10104', '10105', '10106', '10107', '10110', '10111', '10112', '10115', '10118', '10119', '10120', '10121', '10122', '10123', '10128', '10151', '10152', '10153', '10154', '10155', '10158', '10161', '10162', '10165', '10166', '10167', '10168', '10169', '10170', '10171', '10172', '10173', '10174', '10175', '10176', '10177', '10178', '10199', '10270', '10271', '10278', '10279', '10280', '10281', '10282', '10001', '10002', '10003', '10004', '10005', '10006', '10007', '10009', '10010', '10011', '10012', '10013', '10014', '10015', '10016', '10017', '10018', '10019', '10020', '10021', '10022', '10023', '10024', '10025', '10026', '10027', '10028', '10029', '10030', '10031', '10032', '10033', '10034', '10035', '10036', '10037', '10038', '10039', '10040', '10041', '10044', '10045', '10048', '10055', '10060', '10069', '10090', '10095', '10098', '10099', '10103', '10104', '10105', '10106', '10107', '10110', '10111', '10112', '10115', '10118', '10119', '10120', '10121', '10122', '10123', '10128', '10151', '10152', '10153', '10154', '10155', '10158', '10161', '10162', '10165', '10166', '10167', '10168', '10169', '10170', '10171', '10172', '10173', '10174', '10175', '10176', '10177', '10178', '10199', '10270', '10271', '10278', '10279', '10280', '10281', '10282'], 'Staten': ['10301', '10302', '10303', '10304', '10305', '10306', '10307', '10308', '10309', '10310', '10311', '10312', '10314', '10301', '10302', '10303', '10304', '10305', '10306', '10307', '10308', '10309', '10310', '10311', '10312', '10314']} #add zipCodes to the borough object for v in boroughZipDict[boroughOfInterest.name]: boroughOfInterest.addZipcode(Zipcode(v)) #calculate average average_pop = float(boroughOfInterest.population) / (boroughOfInterest.zipCounter) print "The average population of the zip codes in", sys.argv[1], "is", str(average_pop)
""" # from borough import * from borough import Borough # reuse the functions in problem 4 from problem3 import getzippopulation, getboroughzipcode from sys import argv as bname if __name__ == "__main__": # get the population and borough information from the data in dictionary form populationdic = getzippopulation() boroughdic = getboroughzipcode() # turn the input into the captalized form bname = bname[1].split(" ")[0].capitalize() # initialize the object in Borough class BOROUGH = Borough(bname) # put the data stored from two dictions into the object for zp in boroughdic[bname]: if zp in populationdic: BOROUGH.addZipcode(zp) BOROUGH.addPopulation(populationdic[zp]) # use the class function to get the average number print BOROUGH.PopuPerZip()
def makeunique(list): found = [] keep = [] for entry in list: if entry not in found: found.append(entry) keep.append(entry) return keep with open('boroughs.csv') as f: rows = csv.reader(f, delimiter=',') for row in rows: thiszip = Zipcode(row[0]) rowentry = row[1] if (rowentry.lower() == borough.name): borough.addZipcode(thiszip.number) borough.zipcodes = makeunique(borough.zipcodes) with open('zipCodes.csv') as f: rows = csv.reader(f, delimiter=',') rows.next() for row in rows: if (row[10] != ''): rowzip = row[1] rowpop = float(row[10].strip()) for line in borough.zipcodes: if (int(rowzip) == int(line)): borough.addPopulation(rowpop)