473,594 Members | 2,885 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Quickie: converting r"\x2019" to int

Hey guys. This should just be a quickie: I can't figure out how to convert
r"\x2019" to an int - could someone give me a hand please?

Cheers

-Rob
Apr 6 '06 #1
7 2842
Robin Haswell wrote:
Hey guys. This should just be a quickie: I can't figure out how to convert
r"\x2019" to an int - could someone give me a hand please?


Is this what you mean?
In [9]: int(r'\x2019'[2:], 16)
Out[9]: 8217

or maybe you meant this:
In [6]: ord(u'\u2019')
Out[6]: 8217

Kent
Apr 6 '06 #2
In article <44**********@n ewspeer2.tds.ne t>,
Kent Johnson <ke**@kentsjohn son.com> wrote:
Robin Haswell wrote:
Hey guys. This should just be a quickie: I can't figure out how to convert
r"\x2019" to an int - could someone give me a hand please?


Is this what you mean?
In [9]: int(r'\x2019'[2:], 16)
Out[9]: 8217

or maybe you meant this:
In [6]: ord(u'\u2019')
Out[6]: 8217


Or even:
import struct
struct.unpack(" q", "\0\0"+ r'\x2019')[0] 101671307850041 L


Who knows :)

Just
Apr 6 '06 #3
Just wrote:
Robin Haswell wrote:
Hey guys. This should just be a quickie: I can't figure out how to convert
r"\x2019" to an int - could someone give me a hand please?


Is this what you mean?
In [9]: int(r'\x2019'[2:], 16)
Out[9]: 8217

or maybe you meant this:
In [6]: ord(u'\u2019')
Out[6]: 8217


Or even:
>>> import struct
>>> struct.unpack(" q", "\0\0"+ r'\x2019')[0] 101671307850041 L >>>
Who knows :)


I think we can be pretty sure that he didn't mean
import unicodedata unicodedata.num eric(u"\u2019") Traceback (most recent call last):
File "<stdin>", line 1, in ?
ValueError: not a numeric character

though...
unicodedata.nam e(u"\u2019")

'RIGHT SINGLE QUOTATION MARK'

</F>

Apr 6 '06 #4
On Thu, 06 Apr 2006 20:00:13 +0200, Just wrote:
In article <44**********@n ewspeer2.tds.ne t>,
Kent Johnson <ke**@kentsjohn son.com> wrote:
Robin Haswell wrote:
Is this what you mean?
In [9]: int(r'\x2019'[2:], 16)
Out[9]: 8217

or maybe you meant this:
In [6]: ord(u'\u2019')
Out[6]: 8217


Or even:
>>> import struct
>>> struct.unpack(" q", "\0\0"+ r'\x2019')[0] 101671307850041 L >>>

rob@aranea:~$ python
Python 2.4.2 (#2, Sep 30 2005, 21:19:01)
[GCC 4.0.2 20050808 (prerelease) (Ubuntu 4.0.1-4ubuntu8)] on linux2
Type "help", "copyright" , "credits" or "license" for more information. int("\x2019") 19
Something like that. Except with:
int(r"\x2019") Traceback (most recent call last):
File "<stdin>", line 1, in ?
ValueError: invalid literal for int(): \x2019

:-)

-Rob
Apr 7 '06 #5

Robin Haswell wrote:

rob@aranea:~$ python
Python 2.4.2 (#2, Sep 30 2005, 21:19:01)
[GCC 4.0.2 20050808 (prerelease) (Ubuntu 4.0.1-4ubuntu8)] on linux2
Type "help", "copyright" , "credits" or "license" for more information.
int("\x2019") 19
Something like that. Except with:
int(r"\x2019") Traceback (most recent call last):
File "<stdin>", line 1, in ?
ValueError: invalid literal for int(): \x2019

:-)

-Rob


I decided to use this as a learning exercise for myself. This is what I
figured out. All quotes are paraphrased from the docs.

"\xhh in a string substitutes the character with the hex value hh.
Unlike in Standard C, at most two hex digits are accepted."

\x20 is equal to a space. Therefore '\x2019' is equal to ' 19'.

"int(x) converts a string or number to a plain integer. If the argument
is a string, it must contain a possibly signed decimal number
representable as a Python integer, possibly embedded in whitespace."

Therefore int(' 19') is equal to 19.

"When an 'r' prefix is present, a character following a backslash is
included in the string without change, and all backslashes are left in
the string".

Therefore r'\x2019' is left unchanged, and cannot be converted to an
int.

Rob, this explains *why* you are getting the above error. It does not
explain how to achieve your objective, as you have not specified what
it is. If you give more information, one of the resident gurus may be
able to assist you.

Frank Millman

Apr 7 '06 #6

Robin Haswell wrote:
Hey guys. This should just be a quickie: I can't figure out how to convert
r"\x2019" to an int - could someone give me a hand please?

int(r'\x2019'.d ecode('string_e scape'))

19

Apr 7 '06 #7
> Therefore r'\x2019' is left unchanged, and cannot be converted to an
int.

Rob, this explains *why* you are getting the above error. It does not
explain how to achieve your objective, as you have not specified what
it is. If you give more information, one of the resident gurus may be
able to assist you.
Thanks, I think that helps.

Basically I'm decoding HTML character references. "&#x2019" is a character
reference, equal to a single quote (ish).
http://ganesh.bronco.co.uk/example.html is the character in action. I want
to get from the string "x2019" to the Unicode character ’.

However, your help has lead me to a solution!
u"\u2019" u'\u2019' unichr(int("201 9", 16)) u'\u2019'


That's got it - thanks :-)

-Rob
Frank Millman’


Apr 7 '06 #8

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

Similar topics

12
1445
by: yoni | last post by:
Hi, Consider the following XML document: <article> This is a sample <literal>document</literal>. Some <literal>words</literal>, from some reason, are tagged with the <literal>literal</literal> tag. </article>
0
7947
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, well explore What is ONU, What Is Router, ONU & Routers main usage, and What is the difference between ONU and Router. Lets take a closer look ! Part I. Meaning of...
0
7880
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8374
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...
0
8242
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...
1
5739
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 presenter, Adolph Dupr who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5413
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
3868
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
2389
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
0
1217
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.