Created
March 19, 2012 02:48
-
-
Save pcperini/2091808 to your computer and use it in GitHub Desktop.
Port CSV to JSON
This file contains 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 sys | |
import json | |
raw_data = open(sys.argv[1]).read().split('\r\n') | |
csv_headers = raw_data[0].split(',') | |
csv_data = [piece.split(',') for piece in raw_data[1:]] | |
json_data = [] | |
for csv_datum in csv_data: | |
json_datum = {} | |
for csv_header in csv_headers: | |
json_datum[csv_header] = csv_datum[csv_headers.index(csv_header)] | |
json_data += [json_datum] | |
json.dump(json_data, open(sys.argv[1].replace('csv', 'json'), 'w')) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment