Created
February 4, 2017 12:32
-
-
Save ketanbhatt/cb7c440779a75a866465bf2b34c324e4 to your computer and use it in GitHub Desktop.
Converts rows of JSON to CSV
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 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