Skip to content

Instantly share code, notes, and snippets.

@hbillings
Created April 25, 2015 19:58
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 hbillings/e68b6077b15b02e9fb6e to your computer and use it in GitHub Desktop.
Save hbillings/e68b6077b15b02e9fb6e to your computer and use it in GitHub Desktop.
Read a CSV, spit out JSON for D3
import csv
import json
is_first_line = True
input_file_name = "your_file_here.csv"
output_file_name = "arrays.json"
output_json_obj = open(output_file_name, "wb")
with open(input_file_name, "rb") as file:
csv_data = csv.reader(file)
output_json_obj.write("[")
for row in csv_data:
if is_first_line == False:
output_json_obj.write(",")
else:
is_first_line = False
json.dump(row, output_json_obj)
output_json_obj.write("]")
output_json_obj.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment