名称= f'scheduler-test- {stage}'SyntaxError:无效的语法

时间:2020-06-22 17:35:22

标签: python-3.x syntax format

我在下面的代码中收到语法错误。你能帮我解决吗?

#! /usr/bin/python3
import sys
import boto3
client = boto3.client('sns')
if __name__ == '__main__':

  if len(sys.argv) < 2:
     print('Missing argument for stage.')
     exit()
  stage = sys.argv[1]

  name = f'scheduler-test-{stage}'
  print(f'Creating topic {name}')
  create_response = client.create_topic(Name=name,)
  arn = create_response['TopicArn']
  print(f'Created topic {name} with arn {arn}')
  if len(sys.argv) == 3:
        role = sys.argv[2]
  else:
        role = 'arn:aws:sts::256608350746:assumed-role/aws-scheduler-prod-us-east-1-lambdaRole/aws-scheduler-prod-emitter'
  print(f'Granting publish rights to {name} topic for role {role}')

  permission_response = client.add_permission(
        TopicArn=arn,
        Label=f'{name}-publish-access',
        AWSAccountId=[str(role)],
        ActionName=['Publish']
  )
  print('Done')

我遇到错误了

文件“ ./init_output_topic.py”,第18行 名称= f'scheduler-test- {stage}' ^ SyntaxError:语法无效

1 个答案:

答案 0 :(得分:0)

您似乎正在使用python 2运行python 3+语法。在python 3中引入了使用“ f”的字符串格式。

如果要使用python 2,则需要使用旧的格式:

name = 'scheduler-test-%s' % (stage)

https://realpython.com/python-f-strings/

或者仅使用“ python3 script.py”编译代码