473,804 Members | 4,795 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

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ñad o $\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?

Romano

Feb 25 '08 #1
5 2328
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ñad o $\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
Feb 25 '08 #2
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ñad o $\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
Feb 25 '08 #3
On Feb 25, 6:03 pm, "OKB (not okblacke)"
<brenNOSPAMb... @NObrenSPAMbarn .netwrote:
>
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.
Yes, I think I will do something like that, although... I really do
not understand why \x5c is not interpreted in a raw string but \u005c
is interpreted in a unicode raw string... is, well, not elegant. Raw
should be raw...

Thanks anyway

Feb 25 '08 #4
On Feb 26, 8:45 am, rmano <romano.gianne. ..@gmail.comwro te:
BTW, 2to3.py should warn when a raw string (not unicode) with \u in
it, I think.
I tried it and it seems to ignore the problem...
Python 3.0a3+ (py3k:61229, Mar 4 2008, 21:38:15)
[GCC 4.1.3 20070929 (prerelease) (Ubuntu 4.1.2-16ubuntu2)] on linux2
Type "help", "copyright" , "credits" or "license" for more information.
>>r"\u"
'\\u'
>>r"\uparrow"
'\\uparrow'
>>r"\u005c"
'\\u005c'
>>r"\N{REVERS E SOLIDUS}"
'\\N{REVERSE SOLIDUS}'
>>"\u005c"
'\\'
>>"\N{REVERSE SOLIDUS}"
'\\'

2to3.py may be ignoring a problem, but existing raw 8-bit string
literals containing a '\u' aren't going to be it. If anything is going
to have a problem with conversion to Py3k at this point, it is raw
Unicode literals that contain a Unicode escape.
Mar 4 '08 #5
On Mar 4, 1:00 pm, NickC <ncogh...@gmail .comwrote:
>
Python 3.0a3+ (py3k:61229, Mar 4 2008, 21:38:15)
[GCC 4.1.3 20070929 (prerelease) (Ubuntu 4.1.2-16ubuntu2)] on linux2
Type "help", "copyright" , "credits" or "license" for more information.>>r "\u"
'\\u'
>r"\uparrow"
'\\uparrow'
Nice to know... so it seems that the 3.0 doc was not updated. I think
this is the correct
behaviour. Thanks

Mar 7 '08 #6

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

Similar topics

1
2787
by: Clemens Hoffmann | last post by:
Hello, i use resources to store code fragments. This fragments contain sequences with line feed and tabs (i.e: "\r\n\t\t"). When reading the text from the resource it is a literal string (in the debugger it is stated with a @ character in from of it.
2
1798
by: Matt | last post by:
I'm working on a plug-in for an application called DesktopSideBar for Windows. It emulates to some degree the Longhorn sidebar. The SDK for DesktopSideBar supports .NET plug-in development through COM Interop and a provided reference DLL. In the SDK are two included samples... One, called RecycleBin is written entirely in unmanaged C++ as a normal DLL. The second, StickerPanel, is written in C#. My first exercise was porting StickerPanel...
2
2898
by: Anastasios Papadopoulos | last post by:
Hello all, I have statements like the following in my Property Get: Public Property AccountNumber() As String Get Return MyClass.AccountNumber.ToString End Get blah blah End Property
2
3894
by: jindo | last post by:
hi there! I want to service to java web services. so i made web references! but web services operations not made. "style=rpc, use=literal" how to connect java(rpc/literal) with dot net frameworks.
0
1806
by: Steve | last post by:
The code: SQLUPDATE="UPDATE BRDICT.TAUDIT " SQLUPDATE=SQLUPDATE & "SET " SQLUPDATE=SQLUPDATE & "LOG_INIT_UPD = " & "'" & qsLOG_INIT_UPD & "', " SQLUPDATE=SQLUPDATE & "LOG_UID = " & "'" & qsLOG_UID & "', " SQLUPDATE=SQLUPDATE & "PURPOSE = " & "'" & qsPURPOSE & "', " SQLUPDATE=SQLUPDATE & "CHG_DTL = " & "'" & qsCHG_DTL & "', " SQLUPDATE=SQLUPDATE & "SW_SYSTEM = " & "'" & qsSW_SYSTEM & "' " SQLUPDATE=SQLUPDATE & " Where REC_NUM = " & ...
3
6873
by: Jim Hill | last post by:
Well, I've found about a hundred thousand web pages where people have had the same problem I have but nary a page with a solution that works for me. I want to do a simple embed, so I've followed the example in the Extending and Embedding documentation: In the .c file, #include <Python.h>
3
2506
by: =?Utf-8?B?QWxleCBNYWdoZW4=?= | last post by:
I am confused about how I can insert literal text into a page from C# in the ASP.NET 2.0 Mobile Page model. There is no supported "Literal" control as there is in ASPX (standard), though I don't understand why not. So the question is, how do I get literal HTML text into a certain spot on my ASPX page in mobile? It seems I cannot even insert a <pand then run it at the server and control it's InnerText or anything. Help? Alex
5
3278
by: cameljs18 | last post by:
Converting a string variable into a string literal. How do I add the @ character in front of the string? I cannot add it when the string is created as it will affect other parts of the program. I have tried these but they do not work: label1.text = @S label1.text = "@" + S
4
2209
by: Nagrik | last post by:
Dear Group, I understand that if the literal string is bound to a variable then it goes in a special place in the memory and remains with the program till the end and its characters can not be manipulated. But however the following example. Will the literal string (returned to variable retval from function retstr) will remain with the program forever in the following examples.
0
9706
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10577
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10332
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10320
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10077
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9150
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5651
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3820
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2991
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.