Stupid Python Tricks – Unescape a String
Ever been given a pre-escaped string like ‘\\r\\n’ and wanted the actual non-escaped version?
Try this:
>>> ‘\\\\\\\\’.decode(’string_escape’) ‘\\\\’
I just found it today. Pretty cool, eh.
Stupid Python Tricks – Unescape a StringEver been given a pre-escaped string like ‘\\r\\n’ and wanted the actual non-escaped version? Try this: >>> ‘\\\\\\\\’.decode(’string_escape’) ‘\\\\’ I just found it today. Pretty cool, eh. 2 Responses to “Stupid Python Tricks – Unescape a String” |
The given example cannot be used at an interactive prompt. You need
'\\\\'orr'\\'and the output would be'\\'(that is, a single backslash escaped for the repr).@Anonymous, Fixed it. Thanks.