przemyslaw.sagalo
przemyslaw.sagalo

Reputation: 21

python3 - format with {} symbols

I hav egot problem with string

'replication = {\'class\' : \'NetworkTopologyStrategy\', \'datacenter1\' : {}};'.format(N)

Why does it return:

replication = \{\'class\' : \'NetworkTopologyStrategy\', \'datacenter1\': {} };'.format(N)  
KeyError: "'class' "

Upvotes: 1

Views: 43

Answers (1)

DeepSpace
DeepSpace

Reputation: 81594

Formatting a string that contains arbitrary {} can be funky.

In this case you'd need to surround the entire string in additional {} in order to escape the { and } that should be ignored by format:

N = 'xxx'
print('replication = {{\'class\' : \'NetworkTopologyStrategy\', \'datacenter1\' : {}}};'
      .format(N))

# replication = {'class' : 'NetworkTopologyStrategy', 'datacenter1' : xxx};

Upvotes: 1

Related Questions