Skip to content
This repository was archived by the owner on May 13, 2025. It is now read-only.
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions json_to_csv_converter.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,18 @@

def read_and_write_file(json_file_path, csv_file_path, column_names):
"""Read in the json dataset file and write it out to a csv file, given the column names."""
with open(csv_file_path, 'wb+') as fout:
with open(csv_file_path, 'w+') as fout:
csv_file = csv.writer(fout)
csv_file.writerow(list(column_names))
with open(json_file_path) as fin:
with open(json_file_path, encoding='UTF-8') as fin:
for line in fin:
line_contents = json.loads(line)
csv_file.writerow(get_row(line_contents, column_names))

def get_superset_of_column_names_from_file(json_file_path):
"""Read in the json dataset file and return the superset of column names."""
column_names = set()
with open(json_file_path) as fin:
with open(json_file_path, encoding='UTF-8') as fin:
for line in fin:
line_contents = json.loads(line)
column_names.update(
Expand All @@ -49,7 +49,7 @@ def get_column_names(line_contents, parent_key=''):

"""
column_names = []
for k, v in line_contents.iteritems():
for k, v in line_contents.items():
column_name = "{0}.{1}".format(parent_key, k) if parent_key else k
if isinstance(v, collections.MutableMapping):
column_names.extend(
Expand Down Expand Up @@ -93,7 +93,7 @@ def get_row(line_contents, column_names):
line_contents,
column_name,
)
if isinstance(line_value, unicode):
if isinstance(line_value, str):
row.append('{0}'.format(line_value.encode('utf-8')))
elif line_value is not None:
row.append('{0}'.format(line_value))
Expand Down