Professional Web Applications Themes

reading UTF-8 lines and writing to TK text area - Ruby

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 doentation, 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 ...

  1. #1

    Default reading UTF-8 lines and writing to TK text area

    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
    doentation, 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 Guest

  2. #2

    Default Re: reading UTF-8 lines and writing to TK text area

    Hi,

    From: Matthew Huggett <att.ne.jp>
    Subject: reading UTF-8 lines and writing to TK text area
    Date: Fri, 13 Feb 2004 19:52:31 +0900
    Message-ID: <att.ne.jp> 

    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\u3 089\u304C\u306A, ' +
    '\u6F22\u5B57\u3068\u30AB\u30BF\u30AB\u30CA')
    t.insert('end', txt)
    Tk.mainloop
    ----------------------------------------------------------
    --
    Hidetoshi NAGAI (kyutech.ac.jp)


    Hidetoshi Guest

  3. #3

    Default Re: reading UTF-8 lines and writing to TK text area

    Hi,

    From: Matthew Huggett <att.ne.jp>
    Subject: reading UTF-8 lines and writing to TK text area
    Date: Fri, 13 Feb 2004 19:52:31 +0900
    Message-ID: <att.ne.jp> 

    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 (kyutech.ac.jp)


    Hidetoshi Guest

Similar Threads

  1. Writing SUPERIMPOSED over other lines of text
    By Stevan in forum Web Design
    Replies: 5
    Last Post: December 23rd, 01:40 PM
  2. Writing and reading XML?
    By BojanKV webforumsuser@macromedia.com in forum Macromedia Flash Actionscript
    Replies: 2
    Last Post: January 30th, 12:25 PM
  3. Reading from / Writing to text files out of Flash
    By Carl Schubert in forum Macromedia Flash
    Replies: 1
    Last Post: December 30th, 09:10 PM
  4. XML Reading, Writing
    By Jeff Fitzgerald in forum PHP Development
    Replies: 0
    Last Post: September 15th, 01:41 AM
  5. XML reading and writing
    By mikeveli webforumsuser@macromedia.com in forum Macromedia Director Lingo
    Replies: 2
    Last Post: September 2nd, 01:07 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139