Converting string representation of tuples, lists and dicts to its actual representation

Starting in Python 2.6 you can use the built-in ast.literal_eval

>>> import ast

This is safer than using eval. As its own docs say:

>>> help(ast.literal_eval)
Help on function literal_eval in module ast:

literal_eval(node_or_string)
    Safely evaluate an expression node or a string containing a Python
    expression.  The string or node provided may only consist of the following
    Python literal structures: strings, numbers, tuples, lists, dicts, booleans,
    and None.

For more understanding:

:

Post a Comment

1 Comments