1

I hav egot problem with string

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

Why does it return:

replication = \{\'class\' : \'NetworkTopologyStrategy\', \'datacenter1\': {} };'.format(N)  
KeyError: "'class' "
2
  • If you notice, you're not actually escaping your characters. Commented Apr 12, 2018 at 12:25
  • Literal { and } characters in a format string must be escaped: {{ }} Commented Apr 12, 2018 at 12:25

1 Answer 1

1

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};
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you. I have solve it by 'replication = {\'class\' : \'NetworkTopologyStrategy\', \'' + data_center_name + '\' : ' + str(N) + ' }'. But your way is more pretty

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.