Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Single '}' encountered in format string

Tags:

python

mysql

I have a usertable with a column 'routes' that has values like '[{"407": "410"}, {"410": "408"}]'. I need to select all rows that have values 408"}, so I'm using like keyword in MySQL to achieve this. The query looks as below select routes from usertable where routes like '%408"}%'

This query works in MySQL Workbench, but when I take this to Python its not working, my query looks like below in Python (select routes from usertable where routes like'%{1}"}%').format(routes)`

The error I see is as below:

    "Single '}' encountered in format string"

I did try using escape characters i.e. \" and }} but still getting the same error.

Please suggest how to fix this.

Thanks, Seema

like image 337
Seema Avatar asked Jul 08 '26 19:07

Seema


1 Answers

'%{1}"}%'.format(routes)' should be '%{1}"}}%'.format(routes)' (note the double }}).

that is how you can convice format not to try and fill a value but to return just one }. as stated in the doc:

The parts of the string outside curly braces are treated literally, except that any doubled curly braces '{{' or '}}' are replaced with the corresponding single curly brace.

like image 171
hiro protagonist Avatar answered Jul 11 '26 09:07

hiro protagonist



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!