473,397 Members | 1,985 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,397 software developers and data experts.

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 2828
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**********@newspeer2.tds.net>,
Kent Johnson <ke**@kentsjohnson.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] 101671307850041L


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] 101671307850041L >>>
Who knows :)


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

though...
unicodedata.name(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**********@newspeer2.tds.net>,
Kent Johnson <ke**@kentsjohnson.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] 101671307850041L >>>

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'.decode('string_escape'))

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("2019", 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
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...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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
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,...
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...

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.