Embedding a literal "\u" in a unicode raw string.
OKB (not okblacke)
brenNOSPAMbarn at NObrenSPAMbarn.net
Mon Feb 25 18:03:53 CET 2008
Romano Giannetti wrote:
> 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")
> 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
I too encountered this problem, in the same situation (making
strings that contain LaTeX commands). One possibility is to separate
out just the bit that has the \u, and use string juxtaposition to attach
it to the others:
s = ur"aÃ±ado " u"$\\uparrow$"
It's not ideal, but I think it's easier to read than your solution
--OKB (not okblacke)
"Do not follow where the path may lead. Go, instead, where there is
no path, and leave a trail."
More information about the Python-list