Skip to content

Instantly share code, notes, and snippets.

@ketanbhatt
Created February 4, 2017 12:32
Show Gist options
  • Save ketanbhatt/cb7c440779a75a866465bf2b34c324e4 to your computer and use it in GitHub Desktop.
Save ketanbhatt/cb7c440779a75a866465bf2b34c324e4 to your computer and use it in GitHub Desktop.
Converts rows of JSON to CSV
import sys
import csv
import ujson
input_file_name, output_file_name = sys.argv[1:3]
headers = sys.argv[-1].split(',')
print input_file_name,
output_rows = []
with open(input_file_name, 'rb') as input_file:
input_rows = input_file.readlines()
for row in input_rows:
row_dict = ujson.loads(row)
output_rows.append([
repr(row_dict.get(header, '')) for header in headers
])
with open(output_file_name, 'wb') as output_file:
output_file = csv.writer(output_file)
output_file.writerow(headers)
output_file.writerows(output_rows)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment