[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.python

Displaying Unicode Chars

gregpinero@gmail.com

2/10/2008 8:39:00 PM

I want to make a little Python utility where a user can enter the
unicode numerical code and get the actual symbol back in utf-8.

For example, a user could enter something like u221E

And get back 8

Now, this does seem to work:

>>> print u"\u221E"
8
However how can I change it so it works with a string variable?

print unicode("\u221E") doesn't seem to do it.

I hope this makes sense. I don't know all the unicode terminology to
phrase this question coherently ;-)

Thanks in advance,

Greg
5 Answers

Martin v. Loewis

2/10/2008 9:30:00 PM

0

> I want to make a little Python utility where a user can enter the
> unicode numerical code and get the actual symbol back in utf-8.
>
> For example, a user could enter something like u221E

I'm puzzled why the user would enter "u221E" - why not just "221E"?

> And get back â??
>
> Now, this does seem to work:
>
>>>> print u"\u221E"
> â??
> However how can I change it so it works with a string variable?

The problem is not with variables - print can output variables
just fine:

text = u"\u221E"
print text

(where text is a variable holding a Unicode string)

Your problem is with data conversion: how to convert a string
holding a hexadecimal integer number into a Unicode
character whose ordinal is that number?

To do so, you need two steps (assuming you start from "221E")
0. char = "221E"
1. convert that into an integer: char = int(char, 16)
2. convert that into a Unicode character: char = unichr(char)
3. print it: print char

If you insist on the user entering "u221E" instead, you have
to convert that first into a string without the leading u:

0.5 char = char[1:]

HTH,
Martin

Bjoern Schliessmann

2/10/2008 10:32:00 PM

0

gregpinero@gmail.com wrote:

> However how can I change it so it works with a string variable?
>
> print unicode("\u221E") doesn't seem to do it.

Sure, that's because \u only works in unicode strings. You'd need
to "encode" your \u-containing string to a unicode string. Perhaps
this'll help:

>>> def to_unicode(num):
.... character = "\\u"+num
.... return character.decode("unicode-escape")
....
>>> to_unicode("221E")
u'\u221e'
>>> print to_unicode("221E")
â??

(improvements welcome)

Regards,


Björn

--
BOFH excuse #273:

The cord jumped over and hit the power switch.

gregpinero@gmail.com

2/11/2008 4:38:00 AM

0

On Feb 10, 4:30 pm, "Martin v. Löwis" <mar...@v.loewis.de> wrote:
> > I want to make a little Python utility where a user can enter the
> > unicode numerical code and get the actual symbol back in utf-8.
>
> > For example, a user could enter something like u221E
>
> I'm puzzled why the user would enter "u221E" - why not just "221E"?
>
> > And get back 8
>
> > Now, this does seem to work:
>
> >>>> print u"\u221E"
> > 8
> > However how can I change it so it works with a string variable?
>
> The problem is not with variables - print can output variables
> just fine:
>
> text = u"\u221E"
> print text
>
> (where text is a variable holding a Unicode string)
>
> Your problem is with data conversion: how to convert a string
> holding a hexadecimal integer number into a Unicode
> character whose ordinal is that number?
Thanks to both of you. Those approaches make sense. Here's the final
result if you're curious: http://utilitymill.com/utility/Display_Unicode_Cha...

-Greg


> To do so, you need two steps (assuming you start from "221E")
> 0. char = "221E"
> 1. convert that into an integer: char = int(char, 16)
> 2. convert that into a Unicode character: char = unichr(char)
> 3. print it: print char
>
> If you insist on the user entering "u221E" instead, you have
> to convert that first into a string without the leading u:
>
> 0.5 char = char[1:]
>
> HTH,
> Martin

Martin v. Loewis

2/11/2008 5:57:00 AM

0

> Thanks to both of you. Those approaches make sense. Here's the final
> result if you're curious:
http://utilitymill.com/utility/Display_Unicode_Cha...

Not sure what operating system you are using. On Windows, I recommend
that you look at the charmap.exe utility. On Linux, try gucharmap
or kcharselect.

Regards,
Martin

Bjoern Schliessmann

2/11/2008 1:07:00 PM

0

"Martin v. Löwis" wrote:

> On Linux, try gucharmap or kcharselect.

Or gnome-character-map if using Gnome.

Regards,


Björn

--
BOFH excuse #25:

Decreasing electron flux