我需要将多行json(漂亮格式的JSON)作为参数传递给我的python脚本之一。有办法吗?
答案 0 :(得分:0)
通过filespec而不是JSON文件的路径传递
:import json
import sys
filespec = sys.argv[1]
with open(filespec) as f:
data = json.load(f)
print(data)
然后调用此脚本:python myscript.py /path/to/file.json
答案 1 :(得分:0)
将JSON存储在文件中,并将其传递给代码。例如。 data.json 然后可以解析它json.loads(file)
将数据放入prettyfile.json
import sys
import json
def readfile(file):
with open(file, 'r') as f:
data_dict = json.load(f)
// enter code here
if __name__ == "__main__":
read_file(sys.argv[0])
python file.py prettyjson.json