我正在尝试从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"'
答案 0 :(得分:0)
使用.format()
或F弦时,请注意大括号。留下单个{
或}
会被误解。如果要在使用{
或f字符串时打印单个.format()
,则需要添加{{
。
例如
name = 'foo'
print(f'{name} }')
给出语法错误。但是:
name = 'foo'
print(f'{name} }}')
提供所需的输出:>>> foo }
。