Switch branches/tags
Nothing to show
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
56 lines (35 sloc) 1.29 KB
Data Visualization Project
Parse data from an ugly CSV or Excel file, and render it in
JSON, save to a database, and visualize in graph form.
Part I: Taking data from a CSV/Excel file, and return it into a format
that is easier for Python to play with.
Copyright (c) 2013 E. Lynn Root
Distributed under the zlib png license. See LICENSE for details.
import csv
# Put the full path to your CSV/Excel file here
MY_FILE = "../data/sample_sfpd_incident_all.csv"
def parse(raw_file, delimiter):
"""Parses a raw CSV file to a JSON-like object"""
# Open CSV file, and safely close it when we're done
opened_file = open(raw_file)
# Read the CSV data
csv_data = csv.reader(opened_file, delimiter=delimiter)
# Setup an empty list
parsed_data = []
# Skip over the first line of the file for the headers
fields =
# Iterate over each row of the csv file, zip together field -> value
for row in csv_data:
parsed_data.append(dict(zip(fields, row)))
# Close the CSV file
return parsed_data
def main():
# Call our parse function and give it the needed parameters
new_data = parse(MY_FILE, ",")
# Let's see what the data looks like!
print new_data
if __name__ == "__main__":