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:

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

    	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 
#2.


-- 
--OKB (not okblacke)
Brendan Barnwell
"Do not follow where the path may lead.  Go, instead, where there is
no path, and leave a trail."
	--author unknown



More information about the Python-list mailing list