【问题标题】:With, If... statements in python inline code [duplicate]Python内联代码中的With,If ...语句[重复]
【发布时间】:2019-06-23 23:26:18
【问题描述】:

我想从我的 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)' 

我知道我可以使用 openclose 语句,但我想了解为什么 with 语句不起作用。

【问题讨论】:

  • if 有一个语法:"something" if condition else "something else"。因此,python 无法理解您的 if。我假设与with 相同
  • 你为什么对把所有东西都写在一行上这么好奇?多行代码无需任何成本即可增加可读性。
  • 我在这里粘贴的代码是为了了解为什么嵌入在我的 bash 脚本中的代码不起作用。我很清楚“if”和“with”语句的工作原理,请仔细阅读我的问题并查看示例
  • 简单的答案是,您不能将任意 Python 代码放在一行中。一些简单的情况可以使用;进行组合,但一般情况下你需要换行符。
  • 我为您编辑了问题以了解真实情况...

标签: python python-3.x inline


【解决方案1】:

根据我的代码更新,下面的语法有效:

#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 语句在这里不起作用

【讨论】:

    猜你喜欢
    • 2017-10-28
    • 2021-01-09
    • 1970-01-01
    • 1970-01-01
    • 2022-01-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-05-09
    相关资源
    最近更新 更多