Multiline string substitution in Python
Question:
I am trying this out:
account = "11111111111"
tenant_name= "Demo"
project_name= "demo"
sns_access_policy = """{
"Version": "2012-10-17",
"Statement": [
{
"Sid": "0",
"Effect": "Allow",
"Principal": {
"Service": "codestar-notifications.amazonaws.com"
},
"Action": "sns:Publish",
"Resource": "arn:aws:sns:eu-west-1:{account_id}:{tenant_name}-{project_name}-pipeline-monitoring"
}
]
}"""
sns_access_policy = sns_access_policy.replace("{account_id}",account).replace("{tenant_name}",tenant_name).replace("{project_name}",project_name)
This is doing my work, but I am looking for something like f{string_substitution}
:
sns_access_policy = f"""{
"Version": "2012-10-17",
"Statement": [
{
"Sid": "0",
"Effect": "Allow",
"Principal": {
"Service": "codestar-notifications.amazonaws.com"
},
"Action": "sns:Publish",
"Resource": "arn:aws:sns:eu-west-1:{account}:{tenant_name}-{project_name}-pipeline-monitoring"
}
]
}"""
I am getting the following error:
SyntaxError: f-string: expressions nested too deeply
Is there any other way than using .replace()
for this?
Answers:
When you use an f-string all those braces start an expression, but in your case they are just literal braces. You have to use double braces for that.
sns_access_policy = f"""{{
"Version": "2012-10-17",
"Statement": [
{{
"Sid": "0",
"Effect": "Allow",
"Principal": {{
"Service": "codestar-notifications.amazonaws.com"
}},
"Action": "sns:Publish",
"Resource": "arn:aws:sns:eu-west-1:{account}:{tenant_name}-{project_name}-pipeline-monitoring"
}}
]
}}"""
I am trying this out:
account = "11111111111"
tenant_name= "Demo"
project_name= "demo"
sns_access_policy = """{
"Version": "2012-10-17",
"Statement": [
{
"Sid": "0",
"Effect": "Allow",
"Principal": {
"Service": "codestar-notifications.amazonaws.com"
},
"Action": "sns:Publish",
"Resource": "arn:aws:sns:eu-west-1:{account_id}:{tenant_name}-{project_name}-pipeline-monitoring"
}
]
}"""
sns_access_policy = sns_access_policy.replace("{account_id}",account).replace("{tenant_name}",tenant_name).replace("{project_name}",project_name)
This is doing my work, but I am looking for something like f{string_substitution}
:
sns_access_policy = f"""{
"Version": "2012-10-17",
"Statement": [
{
"Sid": "0",
"Effect": "Allow",
"Principal": {
"Service": "codestar-notifications.amazonaws.com"
},
"Action": "sns:Publish",
"Resource": "arn:aws:sns:eu-west-1:{account}:{tenant_name}-{project_name}-pipeline-monitoring"
}
]
}"""
I am getting the following error:
SyntaxError: f-string: expressions nested too deeply
Is there any other way than using .replace()
for this?
When you use an f-string all those braces start an expression, but in your case they are just literal braces. You have to use double braces for that.
sns_access_policy = f"""{{
"Version": "2012-10-17",
"Statement": [
{{
"Sid": "0",
"Effect": "Allow",
"Principal": {{
"Service": "codestar-notifications.amazonaws.com"
}},
"Action": "sns:Publish",
"Resource": "arn:aws:sns:eu-west-1:{account}:{tenant_name}-{project_name}-pipeline-monitoring"
}}
]
}}"""