我试图调整到CSV解析,我发现JSON这里GitHub上。该代码设置为从终端运行,并定义了3个参数:节点,json文件的路径,要创建的csv的路径
我正在尝试修改代码,以便可以调用它从正在编写的另一个python脚本中运行。从我所学的关于在终端机上运行的模块的信息中,if __name__ == "__main__":
我想从另一个python脚本运行它,我需要创建一个像def main()
调用这样的定义,对吗?
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)
这是我用来调用jsontocsv2.py的其他python脚本:
import jsontocsv2
import json
filename = 'test2.csv'
SourceFile = 'carapi.json'
jsontocsv2.main('cars', SourceFile, filename)
这是我遇到的错误:
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
任何人都可以帮助指出正确的方向以解决此问题吗?我对堆栈溢出进行了很多搜索,发现了类似问题的帖子,但是我一直无法弄清楚如何使它起作用。
我能够使代码正常运行。
我要做的就是将global reduced_item()
语句从def reduced_item()
函数移到def main(node, json_file_path, csv_file_path)
我创建的函数。如果说它的全局变量没有定义,那么我不确定为什么这样做。
同样,为什么定义一些东西Global
通常不是一个好主意?如果你们对如何更好地做到这一点有建议,我欢迎您的指导。感谢您的帮助。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句