I am trying to adjust the json to csv parses I found here on GitHub. The code is setup to run from terminal with 3 arguments defined: node, path to json file, path to csv to create
I am trying to modified the code so that I can call it to run from another python script that I am writing. From what I learned about modules that run from Terminal they use if __name__ == "__main__":
but if I want to run it from another python script I need to create a definition like def main()
to call, right?
import sys
import json
import csv
# https://github.com/vinay20045/json-to-csv
##
# Convert to string keeping encoding in mind...
##
def to_string(s):
try:
return str(s)
except:
# Change the encoding type if needed
return s.encode('utf-8')
def reduce_item(key, value):
global reduced_item
# Reduction Condition 1
if type(value) is list:
i = 0
for sub_item in value:
reduce_item(key + '_' + to_string(i), sub_item)
i = i + 1
# Reduction Condition 2
elif type(value) is dict:
sub_keys = value.keys()
for sub_key in sub_keys:
reduce_item(key + '_' + to_string(sub_key), value[sub_key])
# Base Condition
else:
reduced_item[to_string(key)] = to_string(value)
# the module I created and moved the contents of __main__ to here
def main(node, json_file_path, csv_file_path):
# Reading arguments
# node = sys.argv[1]
# json_file_path = sys.argv[2]
# csv_file_path = sys.argv[3]
fp = open(json_file_path, 'r')
json_value = fp.read()
raw_data = json.loads(json_value)
print(raw_data['tag'])
try:
data_to_be_processed = raw_data[node]
except:
data_to_be_processed = raw_data
processed_data = []
header = []
for item in data_to_be_processed:
reduced_item = {}
reduce_item(node, item)
header += reduced_item.keys()
processed_data.append(reduced_item)
header = list(set(header))
header.sort()
with open(csv_file_path, 'a') as f:
writer = csv.DictWriter(f, header, quoting=csv.QUOTE_ALL)
writer.writeheader()
for row in processed_data:
writer.writerow(row)
print ("Just completed writing csv file with %d columns" % len(header))
# if __name__ == "__main__":
# if len(sys.argv) != 4:
# print ("\nUsage: python json_to_csv.py <node_name> <json_in_file_path> <csv_out_file_path>\n")
# else:
# # Reading arguments
# main(sys.argv)
Here is the other python script I am using to call jsontocsv2.py:
import jsontocsv2
import json
filename = 'test2.csv'
SourceFile = 'carapi.json'
jsontocsv2.main('cars', SourceFile, filename)
Here are the errors I'm getting:
Traceback (most recent call last):
File "/Users/Documents/Projects/test.py", line 8, in <module>
jsontocsv2.main('cars', SourceFile, filename)
File "/Users/Documents/Projects/jsontocsv2.py", line 84, in main
reduce_item(node, item)
File "/Users/Documents/Projects/jsontocsv2.py", line 57, in reduce_item
reduce_item(key + '_' + to_string(sub_key), value[sub_key])
File "/Users/Documents/Projects/jsontocsv2.py", line 61, in reduce_item
reduced_item[to_string(key)] = to_string(value)
NameError: name 'reduced_item' is not defined
Can anyone help point in the right direction for how to fix this? I did a lot of searching on the stack overflow and found posts with similar issues, but I have not been able to figure out how to get this to work.