reading UTF-8 lines and writing to TK text area

Discussion in 'Ruby' started by Matthew Huggett, Feb 13, 2004.

  1. Hi:

    I'm pretty new to Ruby and was wondering if it is possible to read lines
    from a UTF-8 encoded file (some Japanese text in this case) and write
    them to a text area in a TK gui. I was able to do this in Python with
    unicode strings (e.g., u'\u-someCharCode') but with my Ruby script the
    text is garbled. I've seen references to UTF-8 in some Ruby
    documentation, but I'm not sure how to proceed. It would be a start if
    I could just get Japanese written to my console (I run the Japanese
    WinXP so it shouldn't be a problem).

    Thanks,

    Matthew
     
    Matthew Huggett, Feb 13, 2004
    #1
    1. Advertising

  2. Hi,

    From: Matthew Huggett <>
    Subject: reading UTF-8 lines and writing to TK text area
    Date: Fri, 13 Feb 2004 19:52:31 +0900
    Message-ID: <>
    > I was able to do this in Python with
    > unicode strings (e.g., u'\u-someCharCode') but with my Ruby script the
    > text is garbled.


    A sample script 'unicodeout.rb' may help you.
    You'll be able to find it at ext/tk/sample/demos-en/ on the Ruby
    source archive.

    For exapmle,
    ----------------------------------------------------------
    require 'tk'
    t = TkText.new.pack
    txt = Tk::UTF8_String('\u65E5\u672C\u8A9E\u306E\u3072\u3089\u304C\u306A, ' +
    '\u6F22\u5B57\u3068\u30AB\u30BF\u30AB\u30CA')
    t.insert('end', txt)
    Tk.mainloop
    ----------------------------------------------------------
    --
    Hidetoshi NAGAI ()
     
    Hidetoshi NAGAI, Feb 14, 2004
    #2
    1. Advertising

  3. Hi,

    From: Matthew Huggett <>
    Subject: reading UTF-8 lines and writing to TK text area
    Date: Fri, 13 Feb 2004 19:52:31 +0900
    Message-ID: <>
    > I'm pretty new to Ruby and was wondering if it is possible to read lines
    > from a UTF-8 encoded file (some Japanese text in this case) and write
    > them to a text area in a TK gui.


    Did you try "Tk.encoding = 'utf-8'"?
    For example,
    --------------------------------------------------------------------
    require 'tk'
    Tk.encoding = 'utf-8'
    txt = TkText.new.pack
    IO.foreach('a_UTF-8_encoded_file') {|line| txt.insert('end', line)}
    Tk.mainloop
    --------------------------------------------------------------------
    --
    Hidetoshi NAGAI ()
     
    Hidetoshi NAGAI, Feb 15, 2004
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Murali
    Replies:
    2
    Views:
    613
    Jerry Coffin
    Mar 9, 2006
  2. Anz
    Replies:
    8
    Views:
    1,090
  3. leo
    Replies:
    1
    Views:
    312
    Bob Lehmann
    Dec 5, 2005
  4. Ryan Mckenzie
    Replies:
    3
    Views:
    135
    George Opritescu
    Nov 20, 2008
  5. Replies:
    2
    Views:
    121
Loading...

Share This Page