0

i am trying to insert two variables which are got from users & text file in json payload. But i am getting Key error

Code

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()))

Error

KeyError: '"ip-address"'
static const
  • 953
  • 4
  • 16

1 Answers1

0

When using .format() or f-strings you need to be more careful with your curly braces. Leaving a single { or } gets misinterpreted. If you want to print a single { when using .format() or f-strings you need to instead add {{.

e.g.

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

gives a syntax error. But:

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

gives the desired output: >>> foo }.

QuantumChris
  • 963
  • 10
  • 21