我想从bash脚本中使用python进行json解析和其他操作,但有一些语法错误我无法理解。 所以我想深入了解为什么这段代码不起作用:
>>> import sys, json; if "foo": print("yes")
File "<stdin>", line 1
import sys, json; if "foo": print("yes")
^
SyntaxError: invalid syntax
在此有效的同时
>>> if "foo": print ("yes"); import sys, os
...
yes
与:
>>> import sys, json; with open("foo.json") as fd: print(fd.read())
File "<stdin>", line 1
import sys, json; with open("papi.json") as fd: print(fd.read())
^
SyntaxError: invalid syntax
这有效:
>>> with open("foo.json") as fd: print(fd.read()); import sys, json
...
{"repositoryName": "REPOSITORY", "triggers": [{"name": "cc-branches-lifecycle", "destinationArn": "arn:aws:lambda:REGION:ACCOUNT:function:lambda-engineering-codepipeline-cc-lifecycle-RELEASE", "customData": "{\"pipeline_exec_function\": \"arn:aws:lambda:REGION:ACCOUNT:function:lambda-engineering-codepipeline-cc-updates-RELEASE\", \"pipeline_name\": \"PIPELINE\", \"bucket\": \"BUCKET\"}", "branches": [], "events": ["createReference", "deleteReference"]}, {"name": "trigger-DEVBRANCH-updates", "destinationArn": "arn:aws:lambda:REGION:ACCOUNT:function:lambda-engineering-codepipeline-cc-updates-RELEASE", "customData": "{\"pipeline_name\": \"PIPELINE\", \"bucket\": \"BUCKET\"}", "branches": ["DEVBRANCH"], "events": ["updateReference"]}]}
这也有效:
>>> import sys, json; fd = open("foo.json"); print(fd.read())
{"repositoryName": "REPOSITORY", "triggers": [{"name": "cc-branches-lifecycle", "destinationArn": "arn:aws:lambda:REGION:ACCOUNT:function:lambda-engineering-codepipeline-cc-lifecycle-RELEASE", "customData": "{\"pipeline_exec_function\": \"arn:aws:lambda:REGION:ACCOUNT:function:lambda-engineering-codepipeline-cc-updates-RELEASE\", \"pipeline_name\": \"PIPELINE\", \"bucket\": \"BUCKET\"}", "branches": [], "events": ["createReference", "deleteReference"]}, {"name": "trigger-DEVBRANCH-updates", "destinationArn": "arn:aws:lambda:REGION:ACCOUNT:function:lambda-engineering-codepipeline-cc-updates-RELEASE", "customData": "{\"pipeline_name\": \"PIPELINE\", \"bucket\": \"BUCKET\"}", "branches": ["DEVBRANCH"], "events": ["updateReference"]}]}
我的原始想法来自于我试图从bash更新aws codecommit存储库触发器的事实。这是真正的命令发出错误:
#aws codecommit get-repository-triggers --repository-name $repository | python -c 'import json, sys; triggers = json.load(sys.stdin).get("triggers", []); with open(os.environ["triggersfile"]) as fp: triggers_doc = json.load(fp); triggers_doc["triggers"].extend(triggers); triggers_doc["triggers"] = list({trigger["name"]:trigger for trigger in triggers_doc["triggers"]}.values()); with open(os.environ["triggersfile"], "w") as fd: json.dump(triggers_doc, fd)'
我知道我可以使用 open 和 close 语句,但是我想了解为什么 with 语句起作用的原因。
答案 0 :(得分:0)
根据我的代码更新,语法如下:
#aws codecommit get-repository-triggers --repository-name $repository | python -c \
'import json, sys, os; triggers = json.load(sys.stdin).get("triggers", []); fp = open(os.environ["triggersfile"], "r"); triggers_doc = json.load(fp); fp.close(); triggers_doc["triggers"].extend(triggers); triggers_doc["triggers"] = list({trigger["name"]:trigger for trigger in triggers_doc["triggers"]}.values()); fd = open(os.environ["triggersfile"], "w");json.dump(triggers_doc, fd, indent=4); fd.close()'
但是我想了解为什么with语句在这里不起作用