diff --git a/json_to_csv_converter.py b/json_to_csv_converter.py index 61c4f9cb..1a40858d 100644 --- a/json_to_csv_converter.py +++ b/json_to_csv_converter.py @@ -12,10 +12,10 @@ 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)) @@ -23,7 +23,7 @@ def read_and_write_file(json_file_path, csv_file_path, 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( @@ -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( @@ -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))