469,328 Members | 1,316 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,328 developers. It's quick & easy.

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 2649
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 discussion thread is closed

Replies have been disabled for this discussion.

By using this site, you agree to our Privacy Policy and Terms of Use.