Displaying Unicode Chars

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • gregpinero@gmail.com

    Displaying Unicode Chars

    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 $B!g(B

    Now, this does seem to work:
    >>print u"\u221E"
    $B!g(B
    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
  • Bjoern Schliessmann

    #2
    Re: Displaying Unicode Chars

    gregpinero@gmai l.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.decod e("unicode-escape")
    ....
    >>to_unicode("2 21E")
    u'\u221e'
    >>print to_unicode("221 E")
    ∞

    (improvements welcome)

    Regards,


    Björn

    --
    BOFH excuse #273:

    The cord jumped over and hit the power switch.

    Comment

    • =?UTF-8?B?Ik1hcnRpbiB2LiBMw7Z3aXMi?=

      #3
      Re: Displaying Unicode Chars

      Thanks to both of you. Those approaches make sense. Here's the final
      result if you're curious:


      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

      Comment

      • Bjoern Schliessmann

        #4
        Re: Displaying Unicode Chars

        "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

        Comment

        Working...