-
Notifications
You must be signed in to change notification settings - Fork 392
/
Copy pathparse.py
55 lines (35 loc) · 1.28 KB
/
parse.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
"""
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 = next(csv_data)
# 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
opened_file.close()
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__":
main()