Skip to content

Instantly share code, notes, and snippets.

@pcperini
Created March 19, 2012 02:48
Show Gist options
  • Save pcperini/2091808 to your computer and use it in GitHub Desktop.
Save pcperini/2091808 to your computer and use it in GitHub Desktop.
Port CSV to JSON
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