'string_escape' in python 3

Cameron Simpson cs at zip.com.au
Fri Apr 6 18:05:06 EDT 2012


On 06Apr2012 16:57, Terry Reedy <tjreedy at udel.edu> wrote:
| On 4/6/2012 1:52 PM, Nicholas Cole wrote:
| > In Python 2 given the following raw string:
| >
| >>>> s = r"Hello\x3a this is a test"
| >
| > the escaping could be removed by use of the following:
| >
| >>>> s.decode('string_escape')
| 
| > In Python 3, however, the only way I can see to achieve the same
| > result is to convert into a byte stream and then back:
| >
| >>>> bytes(s, 'utf-8').decode('unicode_escape')
| >
| > This seems very ugly (and slightly 'wrong').  Is there no way to do
| > this without using bytes?  Have I missed something?
| 
|  >>> eval("'"+s+"'")
| 'Hello: this is a test'

https://xkcd.com/327/
-- 
Cameron Simpson <cs at zip.com.au> DoD#743
http://www.cskk.ezoshosting.com/cs/

Every \item command in item_list must have an optional argument.
        - Leslie Lamport, LaTeX



More information about the Python-list mailing list