Each line of the CSV file contains 42 fields. We can display the lines as markers in a Google map if there are fields for latitude and longitude, or for the address. We’ll use fields 38 and 39.
""" streettrees.py List the sidewalk trees on East Fifth Street in Manhattan, from Second Avenue to First Avenue (building numbers 300 - 399 inclusive). """ import sys import urllib.request import csv #Comma-Separated Values #Database is at #https://data.cityofnewyork.us/Environment/2015-Street-Tree-Census-Tree-Data/pi5s-9p35 url = "https://data.cityofnewyork.us/api/views/5rq2-4hqu/rows.csv" try: infile = urllib.request.urlopen(url) except urllib.error.URLError as error: print(error, file = sys.stderr) sys.exit(1) sequenceOfBytes = infile.read() #Read whole file into one big sequenceOfBytes. infile.close() try: s = sequenceOfBytes.decode("utf-8") #s is a string except UnicodeError as unicodeError: print(unicodeError, file = sys.stderr) sys.exit(1) lines = csv.reader(s.splitlines()) #Building numbers on East Fifth Street between Second and First Avenues #in Manhattan go from 300 to 399 inclusive. lines = [line for line in lines if line[26] == "10003" and line[7] == "Alive" and line[25].endswith("EAST 5 STREET") and int(line[25].split(maxsplit = 1)[0]) >= 300] #Sort in order of increasing street number (from west to east). lines.sort(key = lambda line: int(line[25].split(maxsplit = 1)[0])) for line in lines: latitude = float(line[38]) longitude = float(line[39]) address = line[25] english = line[10] #name of species latin = line[9] print(f"{latitude:11.8f} {longitude:12.8f} {address} {english} ({latin})") sys.exit(0)
40.72658025 -73.98864366 300 EAST 5 STREET Sophora (Styphnolobium japonicum) 40.72668676 -73.98860456 303 EAST 5 STREET Sophora (Styphnolobium japonicum) 40.72637933 -73.98823942 306 EAST 5 STREET Sophora (Styphnolobium japonicum) 40.72646883 -73.98838040 306 EAST 5 STREET Sophora (Styphnolobium japonicum) 40.72655075 -73.98828208 315 EAST 5 STREET Sophora (Styphnolobium japonicum) 40.72633629 -73.98813774 319 EAST 5 STREET Sophora (Styphnolobium japonicum) 40.72628559 -73.98801793 321 EAST 5 STREET Sophora (Styphnolobium japonicum) 40.72624222 -73.98791547 325 EAST 5 STREET Sophora (Styphnolobium japonicum) 40.72617727 -73.98769151 329 EAST 5 STREET Sophora (Styphnolobium japonicum) 40.72629044 -73.98766488 329 EAST 5 STREET Callery pear (Pyrus calleryana) 40.72613223 -73.98758510 333 EAST 5 STREET Sophora (Styphnolobium japonicum) 40.72608553 -73.98747475 334 EAST 5 STREET Sophora (Styphnolobium japonicum) 40.72602515 -73.98733209 336 EAST 5 STREET Sophora (Styphnolobium japonicum) 40.72610837 -73.98723323 339 EAST 5 STREET Callery pear (Pyrus calleryana) 40.72588803 -73.98700814 342 EAST 5 STREET common hackberry (Celtis occidentalis) 40.72584166 -73.98689858 342 EAST 5 STREET common hackberry (Celtis occidentalis) 40.72599951 -73.98697512 345 EAST 5 STREET American hophornbeam (Ostrya virginiana) 40.72593084 -73.98681230 347 EAST 5 STREET American hophornbeam (Ostrya virginiana) 40.72596650 -73.98689686 347 EAST 5 STREET American hophornbeam (Ostrya virginiana)
No longer any reason to
sort
the lines.
Then output the lines to a new CSV file.
The
newline = None
prevents the output lines from being double spaced.
""" streettrees2.py Output a CSV file of the sidewalk trees on East Fifth Street in Manhattan, from Second Avenue to First Avenue (building numbers 300 - 399 inclusive). """ import sys import csv #Comma-Separated Values import urllib.request #Database is at #https://data.cityofnewyork.us/Environment/2015-Street-Tree-Census-Tree-Data/pi5s-9p35 url = "https://data.cityofnewyork.us/api/views/5rq2-4hqu/rows.csv" try: infile = urllib.request.urlopen(url) except urllib.error.URLError as error: print(error, file = sys.stderr) sys.exit(1) sequenceOfBytes = infile.read() #Read whole file into one big sequenceOfBytes. infile.close() try: s = sequenceOfBytes.decode("utf-8") #s is a string except UnicodeError as unicodeError: print(unicodeError, file = sys.stderr) sys.exit(1) inputLines = csv.reader(s.splitlines()) try: outfile = open("streettrees.csv", "w", newline = None) except BaseException as error: print(error, file = sys.stderr) sys.exit(1) writer = csv.writer(outfile) fieldIndices = [38, 39, 25, 10, 9] #Want to output only 5 of the 42 fields. #Read the first line from the input file. It's a line of 42 headers. headers = next(inputLines) #headers is a list of 42 strings. selectedHeaders = [headers[i] for i in fieldIndices] #selectedHeaders is a list of 5 strings. writer.writerow(selectedHeaders) #Building numbers on East Fifth Street between Second and First Avenues #in Manhattan go from 300 to 399 inclusive. outputLines = [ [inputLine[i] for i in fieldIndices] #Create a list of 5 strings. for inputLine in inputLines if inputLine[26] == "10003" and inputLine[7] == "Alive" and inputLine[25].endswith("EAST 5 STREET") and int(inputLine[25].split(maxsplit = 1)[0]) >= 300 ] writer.writerows(outputLines) #outputLines is a list of lists, each containing 5 strings. outfile.close() sys.exit(0)
You should now have a new file named
streettrees.csv
on your Mac containing the following lines,
each containing five comma-separated fields.
Latitude,longitude,address,spc_common,spc_latin 40.72613223,-73.9875851,333 EAST 5 STREET,Sophora,Styphnolobium japonicum 40.72628559,-73.98801793,321 EAST 5 STREET,Sophora,Styphnolobium japonicum 40.72633629,-73.98813774,319 EAST 5 STREET,Sophora,Styphnolobium japonicum 40.72617727,-73.98769151,329 EAST 5 STREET,Sophora,Styphnolobium japonicum 40.72624222,-73.98791547,325 EAST 5 STREET,Sophora,Styphnolobium japonicum 40.72637933,-73.98823942,306 EAST 5 STREET,Sophora,Styphnolobium japonicum 40.72658025,-73.98864366,300 EAST 5 STREET,Sophora,Styphnolobium japonicum 40.72646883,-73.9883804,306 EAST 5 STREET,Sophora,Styphnolobium japonicum 40.72608553,-73.98747475,334 EAST 5 STREET,Sophora,Styphnolobium japonicum 40.72602515,-73.98733209,336 EAST 5 STREET,Sophora,Styphnolobium japonicum 40.72588803,-73.98700814,342 EAST 5 STREET,common hackberry,Celtis occidentalis 40.72584166,-73.98689858,342 EAST 5 STREET,common hackberry,Celtis occidentalis 40.72593084,-73.9868123,347 EAST 5 STREET,American hophornbeam,Ostrya virginiana 40.7259665,-73.98689686,347 EAST 5 STREET,American hophornbeam,Ostrya virginiana 40.72629044,-73.98766488,329 EAST 5 STREET,Callery pear,Pyrus calleryana 40.72599951,-73.98697512,345 EAST 5 STREET,American hophornbeam,Ostrya virginiana 40.72655075,-73.98828208,315 EAST 5 STREET,Sophora,Styphnolobium japonicum 40.72610837,-73.98723323,339 EAST 5 STREET,Callery pear,Pyrus calleryana 40.72668676,-73.98860456,303 EAST 5 STREET,Sophora,Styphnolobium japonicum
Then follow these directions, starting at Import your data. Here’s a summary.
streettrees.csv
file created by your Python program.
spc_common
column
(the English name of the species).
Then press the blue Finish button.
Click on the blue placemarks in the following map. See if you can find the three American hophornbeams.
streettrees.csv
,
send the output to the IDLE window as we always do.
You can then copy the output into a new file named
streettrees.csv
that you create with TextEdit.app.
Create the
outputfile
like this:
outfile = io.StringIO(newline = None) #and import ioJust before you
close
the
outfile
,
say
print(outfile.getvalue())
&
is higher than that of
==
.
That’s why we need parentheses around each comparison.
""" streettrees3.py Output a CSV file of the sidewalk trees on East Fifth Street in Manhattan, from Second Avenue to First Avenue (building numbers 300 - 399 inclusive). """ import sys import pandas as pd #Database is at #https://data.cityofnewyork.us/Environment/2015-Street-Tree-Census-Tree-Data/pi5s-9p35 url = "https://data.cityofnewyork.us/api/views/5rq2-4hqu/rows.csv" try: df = pd.read_csv(url) #df is a pandas DataFrame. except BaseException as error: print(error, file = sys.stderr) sys.exit(1) try: outfile = open("streettrees.csv", "w", newline = None) except BaseException as error: print(error, file = sys.stderr) sys.exit(1) #Add two columns to the DataFrame. df[["number", "street"]] = df["address"].str.split(n = 1, expand = True) #like Python str split df["number"] = pd.to_numeric(df["number"], errors = "coerce") #coerce to NaN mask = ( #mask is a pandas Series containing Trues and Falses. (df["zipcode"] == 10003) #or (df.zipcode == 10003) & (df["status"] == "Alive") & (df["street"] == "EAST 5 STREET") & (df["number"] >= 300) ) columns = ["Latitude", "longitude", "address", "spc_common", "spc_latin"] df[mask].to_csv(outfile, columns = columns, header = True, index = False) outfile.close() sys.exit(0)
Latitude,longitude,address,spc_common,spc_latin 40.72613223,-73.98758509999999,333 EAST 5 STREET,Sophora,Styphnolobium japonicum 40.72628559,-73.98801793,321 EAST 5 STREET,Sophora,Styphnolobium japonicum 40.72633629,-73.98813774,319 EAST 5 STREET,Sophora,Styphnolobium japonicum 40.72617727,-73.98769151,329 EAST 5 STREET,Sophora,Styphnolobium japonicum 40.72624222,-73.98791547,325 EAST 5 STREET,Sophora,Styphnolobium japonicum 40.72637933,-73.98823942,306 EAST 5 STREET,Sophora,Styphnolobium japonicum 40.72658025,-73.98864366,300 EAST 5 STREET,Sophora,Styphnolobium japonicum 40.72646883,-73.9883804,306 EAST 5 STREET,Sophora,Styphnolobium japonicum 40.72608553,-73.98747475,334 EAST 5 STREET,Sophora,Styphnolobium japonicum 40.72602515,-73.98733209,336 EAST 5 STREET,Sophora,Styphnolobium japonicum 40.72588803,-73.98700814,342 EAST 5 STREET,common hackberry,Celtis occidentalis 40.72584166,-73.98689858,342 EAST 5 STREET,common hackberry,Celtis occidentalis 40.72593084,-73.9868123,347 EAST 5 STREET,American hophornbeam,Ostrya virginiana 40.7259665,-73.98689686,347 EAST 5 STREET,American hophornbeam,Ostrya virginiana 40.72629044,-73.98766488,329 EAST 5 STREET,Callery pear,Pyrus calleryana 40.72599951,-73.98697512,345 EAST 5 STREET,American hophornbeam,Ostrya virginiana 40.72655075,-73.98828208,315 EAST 5 STREET,Sophora,Styphnolobium japonicum 40.72610837,-73.98723323,339 EAST 5 STREET,Callery pear,Pyrus calleryana 40.72668676,-73.98860456,303 EAST 5 STREET,Sophora,Styphnolobium japonicum