Created
February 12, 2019 18:20
-
-
Save MrJeremyHobbs/664261e287680de60ee2dd548a5db4e7 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import csv | |
import re | |
with open('Main_Libraries__Branches__and_Bookmobiles__FY_2014_Public_Libraries_Survey__Outlet_Data_.csv', encoding="utf-8") as data: | |
reader = csv.reader(data) | |
report_rows = [] | |
# change header | |
header = [ | |
"FSCS ID", | |
"FSCS ID_SEQ", | |
"LIBRARY ID", | |
"LIBRARY NAME", | |
"PHONE", | |
"COUNTY", | |
"LOCATION", | |
"LOCALE", | |
"CITY", | |
"ZIP CODE", | |
"GPS", | |
"COUNTY POPULATION", | |
"OUTLET TYPE", | |
"SQ FEET", | |
"BOOKMOBILES", | |
"HOURS", | |
"WEEKS OPEN", | |
"METRO STATUS", | |
"STRUCTURE CHANGE", | |
"NAME CHANGE", | |
"ADDRESS CHANGE", | |
] | |
report_rows.append(header) | |
# skip header | |
next(reader) | |
# parse data and add GPS co-ordinates | |
for row in reader: | |
# get GPS coordinates | |
location = row[6].split("\n") | |
try: | |
gps = location[2] | |
except IndexError: | |
gps = "" | |
# remove blank GPS coordinates | |
if gps == "(0.0, 0.0)": | |
gps = "" | |
# get longitude and latitude | |
gps_split = gps.split(",") | |
try: | |
longitude = gps_split[0] | |
latitude = gps_split[1] | |
except: | |
longitude = "" | |
latitude = "" | |
# clean up lt and ln | |
longitude = longitude.replace('(', '') | |
latitude = latitude.replace(')', '') | |
# get city and zip | |
try: | |
city = location[1] | |
except IndexError: | |
city = "" | |
# get zip code | |
zips = re.findall("(\d{5})", city) | |
try: | |
zipcode = zips[0] | |
except: | |
zipcode = "" | |
# add to report rows | |
report_row = [ | |
row[0], | |
row[1], | |
row[2], | |
row[3], | |
row[4], | |
row[5], | |
row[6], | |
row[7], | |
f"{city}", | |
f"{zipcode}", | |
f"{gps}", | |
row[8], | |
row[9], | |
row[10], | |
row[11], | |
row[12], | |
row[13], | |
row[14], | |
row[15], | |
row[16], | |
row[17], | |
] | |
report_rows.append(report_row) | |
# output | |
with open('Main_Libraries__Branches__and_Bookmobiles__FY_2014_Public_Libraries_Survey__Outlet_Data_ENHANCED.csv', 'w', encoding="utf-8", newline='\n') as report: | |
writer = csv.writer(report, delimiter =",", quotechar='"',) | |
writer.writerows(report_rows) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment