Use ast.literal_eval :
It is safe to evaluate a node expression or a string containing a Python expression. A string or node can contain only the following literary Python structures: strings, numbers, tuples, lists, dicts, booleans, and None.
This can be used to safely evaluate strings containing Python expressions from untrusted sources without having to analyze the values themselves.
Example:
>>> some_string = '{123: False, 456: True, 789: False}' >>> import ast >>> ast.literal_eval(some_string) {456: True, 123: False, 789: False}
source share