Suppose I have a string which is a backslash-escaped version of another string. Is there an easy way, in Python, to unescape the string? I could, for example, do:
>>> escaped_str = \'\"Hello,\\\\nworld!\"\'
>>> raw_str = eval(escaped_str)
>>> print raw_str
Hello,
world!
>>>
However that involves passing a (possibly untrusted) string to eval() which is a security risk. Is there a function in the standard lib which takes a string and produces a string with no security implications?
>>> print \'\"Hello,\\\\nworld!\"\'.decode(\'string_escape\')
\"Hello,
world!\"
You can use ast.literal_eval
which is safe:
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. (END)
Like this:
>>> import ast
>>> escaped_str = \'\"Hello,\\\\nworld!\"\'
>>> print ast.literal_eval(escaped_str)
Hello,
world!
In python 3, str
objects don\'t have a decode
method and you have to use a bytes
object. ChristopheD\'s answer covers python 2.
# create a `bytes` object from a `str`
my_str = \"Hello,\\\\nworld\"
# (pick an encoding suitable for your str, e.g. \'latin1\')
my_bytes = my_str.encode(\"utf-8\")
# or directly
my_bytes = b\"Hello,\\\\nworld\"
print(my_bytes.decode(\"unicode_escape\"))
# \"Hello,
# world\"