0

I have a problem, I have this string :

{70: True, 956: False, 727: False}

That I want to convert in dictionary. JSon.loads doesn't work with Int values in key.

Someone has any idea?

I tried JSON Loads and AST library and it doesn't work.

1 Answers1

0

As this looks to be a literal Python dict (given the spelling of the bool), ast.literal_eval is likely what you want, its entire purpose being to parse Python literals somewhat safely (it actually parses a bit more as e.g. set() will work, despite not strictly speaking being a literal).

You say that "AST library doesn't work" but you don't actually explain what you tried, or which error you got (generally useful information to provide), and it works fine for me:

ast.literal_eval("{70: True, 956: False, 727: False}")
{70: True, 956: False, 727: False}
Masklinn
  • 34,759
  • 3
  • 38
  • 57