尝试在打印中插入多个变量以检查变量是否正确插入

时间:2019-07-02 13:29:04

标签: python-3.x

我正在尝试从json有效负载中插入来自用户和文本文件的两个变量。但是我遇到了关键错误

代码

print ('{"ip-address": "x.x.x.x","user-name": "john","password": "{}","db-name": "{}","service-name": "Sql","port": #"000","connection-string": "xxx"}'.format(Pass,x.strip()))

错误

KeyError: '"ip-address"'

1 个答案:

答案 0 :(得分:0)

使用.format()或F弦时,请注意大括号。留下单个{}会被误解。如果要在使用{或f字符串时打印单个.format(),则需要添加{{

例如

name = 'foo'
print(f'{name} }')

给出语法错误。但是:

name = 'foo'
print(f'{name} }}')

提供所需的输出:>>> foo }