Embedding a literal "\u" in a unicode raw string.

Diez B. Roggisch deets at nospam.web.de
Mon Feb 25 07:46:38 EST 2008


Romano Giannetti wrote:

> Hi,
> 
> while writing some LaTeX preprocessing code, I stumbled into this problem:
> (I have a -*- coding: utf-8 -*- line, obviously)
> 
> s = ur"añado $\uparrow$"
> 
> Which gave an error because the \u escape is interpreted in raw unicode
> strings, too. So I found that the only way to solve this is to write:
> 
> s = unicode(r"añado $\uparrow$", "utf-8")
> 
> or
> 
> s = ur"añado $\u005cuparrow$"
> 
> The second one is too ugly to live, while the first is at least
> acceptable; but looking around the Python 3.0 doc, I saw that the first
> one will fail, too.
> 
> Am I doing something wrong here or there is another solution for this?

Why don't you rid yourself of the raw-string? Then you need to do 

s = u"anando $\\uparrow$"

which is considerably easier to read than both other variants above.

Diez



More information about the Python-list mailing list