Skip to content

Instantly share code, notes, and snippets.

@coolgarifTech
Last active December 21, 2015 10:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save coolgarifTech/6293345 to your computer and use it in GitHub Desktop.
Save coolgarifTech/6293345 to your computer and use it in GitHub Desktop.
An example python script to show how to parse CSV data into JSON. Used as part of project BorisBoard (http://www.borisboard.com).
# Part of BORIS BOARD Project
# Visit http://www.borisboard.com for more details
#
# By Coolgarif Tech Ltd
# Author: Richie Barter
# 21 Aug 2013
# Sample script for parsing a CSV into JSON for data vis
# Underlying DataSet
# Available here: http://data.london.gov.uk/datastore/package/street-trees-borough
import csv, os.path, json
# Method to handle blank datafields
# And to convert String to Int for JSON parsing
def stringSetup(someNumber):
if someNumber == '' :
return 0
elif someNumber == '!':
return someNumber
else: return int(someNumber)
readFilePath = 'INSERT_FILE_PATH_TO_DATAFILE_HERE'
readFileName = 'WHATEVER_FILENAME_YOU_CHOOSE.csv'
aggReadFile = readFilePath + readFileName
ifile = open(aggReadFile, 'rb')
reader = csv.reader(ifile)
streetTreesByBorough = [ ]
# Reads in a row of the CSV, parses it and
# _appends it to the JSON Structure being created
# Ignores the first row of the CSV due to headers
rowId = 0
for row in reader:
if rowId == 0:
rowId = rowId + 1
continue
boroughName = row[1]
boroughId = row[0]
estTotalStreetTrees2011 = stringSetup(row[3].replace(",",""))
treesPlanted2009to10 = stringSetup(row[4].replace(",",""))
treesFelled2009to10 = stringSetup(row[5].replace(",",""))
mayorTreesPlanted = stringSetup(row[7].replace(",",""))
treesPlanted2009to10 = treesPlanted2009to10 + mayorTreesPlanted
print boroughName
print treesPlanted2009to10
jsonResult = {"boroughName" : boroughName,
"data" :
{"estTotalStreetTrees2011" : estTotalStreetTrees2011,
"treesPlanted2009to10" : treesPlanted2009to10,
"treesFelled2009to10" : treesFelled2009to10
}
}
streetTreesByBorough.append(jsonResult)
streetTreesByBorough = json.dumps(streetTreesByBorough)
ifile.close()
writeFilePath = 'INSERT_FILE_PATH_TO_FinalFILE_HERE'
writeFileName = 'WHATEVER_FILENAME_YOU_CHOOSE.json'
aggWriteFile = writeFilePath + writeFileName
f = open(aggWriteFile, 'w')
f.write(streetTreesByBorough)
f.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment