#Aliya Merali #Assignment 2 #Problem 4 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
if userBorough == "staten island": # if its staten island convert userBorough = "staten" # check that a valid borough was supplied acceptableBoroughs = ["manhattan", "brooklyn", "queens", "bronx", "staten"] if userBorough not in acceptableBoroughs: print "ERROR: please enter a known borough" print acceptableBoroughs sys.exit() # 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:
boroughcon = csv.reader(boroughfile, delimiter=',') popfile = open('zipCodes.csv', 'r') popcon = csv.reader(popfile, delimiter=',') # remove the duplicates def clean(list): checked = [] for x in list: if x not in checked: checked.append(x) return checked # make a dict boroughs['Manhattan'] = Borough('Manhattan') boroughs['Brooklyn'] = Borough('Brooklyn') boroughs['Queens'] = Borough('Queens') boroughs['Bronx'] = Borough('Bronx') boroughs['Staten'] = Borough('Staten') # find the zipcodes in each borough for zipline in boroughcon: zipcode = zipline[0] for x in boroughs.keys(): if zipline[1] == x: boroughs[x].addZipcode(zipcode) # clean for x in boroughs.keys(): boroughs[x].zipcodes = clean(boroughs[x].zipcodes) # calculate the population of each borough popcon.next()
b1=list(boro) zips=csv.reader(open("zipCodes.csv","rb"),delimiter=',') z3=list(zips) #dictionary holding all zip codes' population from zipcodes.csv zippop={} #dictionary for borough, takes user input boroughs={userboro:0} #fill in zipcode for i in range(1,len(z3)): zippop[z3[i][0]]=z3[i][10] # instantiate Borough class in the dictionary # class has 3 parameters, name, zipcodes as an array and sum of population in zip codes in the borough boroughs[userboro]=Borough(userboro) boroughs[userboro].zipcodes=[] boroughs[userboro].sumpopul=0 #first check if in boroughs.csv zip exists #if we find user defined borough name proceed to zip population dictionary #append the zip code to the class object #zort is a list as (zipcode, population in the zipcode) for i in range(0,len(b1)): if b1[i][1] == userboro: zipcode = Zipcode(int(b1[i][0])) if str(zipcode.number) in zippop.keys(): if zippop[str(zipcode.number)]!='':
# Compute the average population of the zip codes in a given borough import sys from zipcode import Zipcode from borough import Borough targetBorough = Borough(sys.argv[1].split(" ")[0]) fileBorough = open('boroughs_tr.csv','r') linesBorough = [] for line in fileBorough: linesBorough.append(line) fileBorough.close() zipBoroughDict = {} # create dictionary as in problem3.py num_linesBorough = len(linesBorough) 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)
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()
import sys import csv import collections #from zip import Zipcode from borough import Borough #import and open the files bor = sys.argv[1] filezipCode = open('zipCodes.csv') csvreaderzc = csv.reader(filezipCode, delimiter=',') fileboroughs = open('boroughs.csv') csvreaderb = csv.reader(fileboroughs, delimiter=',') seq = csvreaderb BorObj = Borough(bor) #csvreaderb = csv.reader(fileboroughs, ['namezip','namebor']) #counting population per zipcode population = {} count = 0 for line in csvreaderzc: if count > 0 and line[10] != '': population[line[1]] = line[10] count = count + 1 #print population #tried to add the population for the whole borough but wasn't working and was #messing up the rest - seems to be the same issue I'm having in problem #3 popMan = 0
#Kara Leary #Urban Informatics #Assignment 2 - Problem 4 import sys import csv from zipcode import Zipcode from borough import Borough #In this code, first I import the name of the borough from the command line. I assume that Staten Island is being input simply as 'Staten'. I set the input to the name of the Borough object 'borough'. I define the same 'makeunique' function that I used in problem 3. Next, I opened 'boroughs.csv' and read through each entry row by row. I set the zip code of the entry to 'thiszip', and take the name of the borough listed. I make the name lowercase as well, so that I can match it to the inputted borough without having to worry about differences in capitalization. If the name matches the one inputted by the user, I use the 'borough.addZipcode method to add the zipcode to the list of that borough's zips. I then employ the 'makeunique' function defined earlier to weed out any duplicates in that boroughs zipcode dictionary. I then go through the file 'zipCodes.csv' row by row. First I ensure that I am only looking at rows that have an entry for population; otherwise it is useless to me. If the row has a population entry, I set the rowzip to that row's zipcode, and rowpop to that row's population. i then go through the entries in the zipcode dictionary: if the row zipcode matches an entry in the dictionary, then it must be part of that borough, and I use the 'addPopulation' method to add the population to the borough's running total. Finally, I use the borough.findAverage function to compute the average population of the zipcodes. boroughname = sys.argv[1] borough = Borough(boroughname.lower()) 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)