reading UTF-8 lines and writing to TK text area

M

Matthew Huggett

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
 
H

Hidetoshi NAGAI

Hi,

From: Matthew Huggett <[email protected]>
Subject: reading UTF-8 lines and writing to TK text area
Date: Fri, 13 Feb 2004 19:52:31 +0900
Message-ID: said:
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
 
H

Hidetoshi NAGAI

Hi,

From: Matthew Huggett <[email protected]>
Subject: reading UTF-8 lines and writing to TK text area
Date: Fri, 13 Feb 2004 19:52:31 +0900
Message-ID: said:
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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top