在shell脚本中在一行中编写多行python代码

时间:2017-03-14 19:56:25

标签: python json shell

我正在尝试使用shell脚本编辑json文件而不使用jq。我找到了必要的python代码,Python read JSON file and modify,但是当我尝试在shell中的一行中执行所有这些代码时,我收到语法错误。 我写了以下命令:

export data = `python -c "import json;import os;filename='test.json';with open(filename, 'r') as f:data = json.load(f) data['id'] = 'abc';os.remove(filename);with open(filename, 'w') as f:json.dump(data, f, indent=4)"`

有什么建议吗?

1 个答案:

答案 0 :(得分:0)

得到答案:

$(python -c "import json;import os;filename='test.json';f=open(filename, 'r');data=json.load(f);data['id']="abc";os.remove(filename);f=open(filename, 'w');json.dump(data, f, indent=4);")

非常感谢!