[UNICODE] Tkinter and Unicode->ISO-8859-1 issues

Martin von Loewis loewis at informatik.hu-berlin.de
Fri Jan 11 09:15:37 EST 2002


vfs at mail.com (Valter Ferraz Sanches) writes:

> example, and try to write again the same string or other that contain
> latin1 stuff, I´m not capable of reading the text file correctly (I
> see the Unicode stuff broken in one byte chars; this way, "ç" becomes
> "ç" and so on)

If you load a file that contains non-ASCII characters into a Tk
widget, I recommend to insert it as a Unicode string, instead of as a
byte string (UTF-8 or otherwise). That means, do

  data = open("file.txt").read()
  data = unicode(data, "latin-1")
  widget['text'] = data

  ...

  data = widget['text']
  data = data.encode('latin-1')
  open("file.txt",'w').write(data)

instead of

  data = open("file.txt").read()
  widget['text'] = data

  ...

  data = widget['text']
  open("file.txt",'w').write(data)

That, of course, assumes you want to use latin-1 in your file.

Hope this helps,
Martin



More information about the Python-list mailing list