How to read strings cantaining escape character from a file and use it as escape sequences?

Duncan Booth duncan.booth at invalid.invalid
Sun Dec 2 06:39:30 EST 2007


John Machin <sjmachin at lexicon.net> wrote:

> Hmmm ... the encode is documented as "Produce a string that is
> suitable as Unicode literal in Python source code", but it *isn't*
> suitable. A Unicode literal is u'blah', this gives just blah. Worse,
> it leaves the caller to nut out how to escape apostrophes and quotes:
> 
>>>> test = u'Python\'\'\'\'\"\"\"\"\u1234\n'
>>>> print repr(test)
> u'Python\'\'\'\'""""\u1234\n'
>>>> print test.encode('unicode-escape')
> Python''''""""\u1234\n
>>>>
> 
> Why would someone bother writing this codec when repr() does the job
> properly?
> 
I don't know why it was written, but if it helps I can tell you why I have 
had occasion to use it: precisely because it does leave the caller to 'nut 
out how to escape apostrophes and quotes'.

repr() does a good enough job if you just want a Python source string, but 
you can't control whether repr will escape quotes or apostrophes - if the 
string contains an apostrophe and no double-quote then the repr will 
enclose it in double-quotes, otherwise it always uses single quotes.

>>> u'"', u'"\'', u'\'"', u'\''
(u'"', u'"\'', u'\'"', u"'")

If you want to force a particular quoting convention then unicode-escape 
gets you half way there and you can get the rest of the way with a couple 
of replace calls.



More information about the Python-list mailing list