'string_escape' in python 3

Terry Reedy tjreedy at udel.edu
Fri Apr 6 18:19:04 EDT 2012


On 4/6/2012 6:05 PM, Cameron Simpson wrote:
> On 06Apr2012 16:57, Terry Reedy<tjreedy at udel.edu>  wrote:
> | On 4/6/2012 1:52 PM, Nicholas Cole wrote:

> |>>>>  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/

I actually thought of that, but assumed that adding enclosing quotes 
would be safe (or that the OP trusted the string). After sending, I 
realized that if Nasty Hacker guessed that the string would be so 
augmented, then it would not be safe. This or above with literal_eval is.

 >>> ast.literal_eval("'{}'".format('\x3a'))
':'

---
Terry Jan Reedy



More information about the Python-list mailing list