如果。。。python内联代码中的语句
我想使用bash脚本中的python来解析json和其他东西,但有一些语法错误我不能真正理解。 所以我想深入理解为什么这段代码不起作用:如果。。。python内联代码中的语句,python,python-3.x,inline,Python,Python 3.x,Inline,我想使用bash脚本中的python来解析json和其他东西,但有一些语法错误我不能真正理解。 所以我想深入理解为什么这段代码不起作用: >>> import sys, json; if "foo": print("yes") File "<stdin>", line 1 import sys, json; if "foo": print("yes") ^ SyntaxError: invalid synta
>>> import sys, json; if "foo": print("yes")
File "<stdin>", line 1
import sys, json; if "foo": print("yes")
^
SyntaxError: invalid syntax
同:
>>> 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
这也适用于:
>>> 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语句不起作用。根据我的代码更新,下面的语法有效:
#aws codecommit get repository触发器--存储库名称$repository | python-c\
'导入json、sys、os;触发器=json.load(sys.stdin).get(“触发器”,[]);fp=开放(os.environ[“triggersfile”],“r”);触发器_doc=json.load(fp);fp.close();触发器文件[“触发器”]。扩展(触发器);triggers_doc[“triggers”]=list({trigger[“name”]:triggers_doc[“triggers”]}.values()中触发器的触发器);fd=开放(os.environ[“triggersfile”],“w”);dump(触发器\u doc,fd,indent=4);fd.close()
但我想理解为什么with语句在这里不起作用if有一个语法:
“something”if条件else“something”
。因此,您的if
对于python
来说是不可理解的。我想,如果是同一个故事,你为什么对把所有东西都写在一行上如此好奇?多行代码以零成本增加可读性。我在这里粘贴的代码是为了理解为什么嵌入在bash脚本中的代码不起作用。我很清楚“if”和“with”语句是如何工作的,请仔细阅读我的问题并查看示例。简单的答案是,不能将任意Python代码放在一行上。使用可以组合一些简单的情况代码>,但一般来说,你需要换行。我为你编辑了这个问题以了解真实情况。。。
>>> 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"]}]}
#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)'