473,412 Members | 2,087 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,412 software developers and data experts.

Unicode raw string containing \u

I'm trying to write a unicode raw string literal, and I seem to be
running up against a conflict between the \uXXXX unicode character
escape and the need to have a literal \u (i.e., backslash followed by a
lowercase letter U) in the string.

If I do ur"\universe" I get a UnicodeDecodeError because (I think)
it tries to interpret \universe as a Unicode escape. But if I do
ur"\\universe" I get a string that contains two backslashes followed by
the word "universe".

How can I specify a unicode raw string literal that contains a
single backslash followed by the word "universe"?

--
--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
Oct 28 '07 #1
3 2299
On Sun, 28 Oct 2007 06:58:48 +0000, OKB (not okblacke) wrote:
I'm trying to write a unicode raw string literal, and I seem to be
running up against a conflict between the \uXXXX unicode character
escape and the need to have a literal \u (i.e., backslash followed by a
lowercase letter U) in the string.

If I do ur"\universe" I get a UnicodeDecodeError because (I think)
it tries to interpret \universe as a Unicode escape. But if I do
ur"\\universe" I get a string that contains two backslashes followed by
the word "universe".
That's because in a raw string, \\ means two backslashes.
How can I specify a unicode raw string literal that contains a
single backslash followed by the word "universe"?
The usual way.
>>word = u'\\universe'
len(word)
9
>>word[0]
u'\\'
>>word[1]
u'u'
>>print word
\universe
>>word
u'\\universe'
--
Steven.

Oct 28 '07 #2
Steven D'Aprano wrote:
> How can I specify a unicode raw string literal that
contains a
single backslash followed by the word "universe"?

The usual way.
>>>word = u'\\universe' len(word) 9 word[0] u'\\' word[1] u'u'
print word \universe word u'\\universe'
That doesn't answer my question, since I asked for a unicode RAW
string literal. Is this not possible? (I was able to get what I want
using ur"\u005Cuniverse", although this is not totally ideal.)

--
--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
Oct 28 '07 #3
That doesn't answer my question, since I asked for a unicode RAW
string literal. Is this not possible? (I was able to get what I want
using ur"\u005Cuniverse", although this is not totally ideal.)
It's a design flaw in Unicode raw string literals that they still
interpret \u escapes. And yes, your notation is one way to get what
you want; another is u"\\"+r"universe", although I'm unsure whether
that meets your requirements.

Regards,
Martin
Oct 28 '07 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: nico | last post by:
In my python scripts, I use a lot of accented characters as I work in french. In order to do this, I put the line # -*- coding: UTF-8 -*- at the beginning of the script file. Then, when I need...
3
by: Shrii | last post by:
1.I read a unicode file by using codec 2.I want to pass that string to exec() statement 3.But one of my character (U+0950) in that string is not showing properly in the output got by that exec()...
1
by: olsongt | last post by:
I was going to submit to sourceforge, but my unicode skills are weak. I was trying to strip characters from a string that contained values outside of ASCII. I though I could just encode as 'ascii'...
2
by: aurora | last post by:
I have some unicode string with some characters encode using python notation like '\n' for LF. I need to convert that to the actual LF character. There is a 'unicode_escape' codec that seems to...
5
by: wanghz | last post by:
Hello, everyone. I have a problem when I'm processing unicode strings. Is it possible to get the 8bit-string representation of any unicode string? Suppose I get a unicode string: a =...
7
by: pattreeya | last post by:
Hello, how can I get the number of byte of the string in python? with "len(string)", it doesn't work to get the size of the string in bytes if I have the unicode string but just the length. (it...
8
by: Preben Randhol | last post by:
Hi If I use len() on a string containing unicode letters I get the number of bytes the string uses. This means that len() can report size 6 when the unicode string only contains 3 characters...
1
by: willie | last post by:
>willie wrote: wrote:
5
by: Frank Stajano | last post by:
A simple unicode question. How do I print? Sample code: # -*- coding: utf-8 -*- s1 = u"héllô wórld" print s1 # Gives UnicodeEncodeError: 'ascii' codec can't encode character # u'\xe9' in...
0
by: Jean-Paul Calderone | last post by:
On Mon, 5 May 2008 16:05:08 +0200, Simon Posnjak <sposnjak@gmail.comwrote: You need to provide some more information about `some_module.some_thing´. How is it implemented? What Python type does...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.