IDLE and non-ascii encoding workaround?

klappnase klappnase at web.de
Sun Nov 16 20:40:14 EST 2003


Hello everyone,

there have already been a couple of threads about IDLE's sometimes
annoying disability to use special characters (in my case I would like
to use for example german umlauts like "ä" etc.).
I have made a workaround that *seems* to work, so I can at least store
files in the editor that use such characters ( on the python shell I
still get:

  UnicodeError: ASCII encoding error: ordinal not in range(128)

but that does not matter much to me).
However I don't know very much about encoding issues, so maybe someone
can tell me if there might be problems that I don't see at the moment.
Here's what I did:

I changed one line in the writefile function in IOBinding.py into a
try-except statement. My function now looks like this:

    def writefile(self, filename):
        self.fixlastline()
        #chars = str(self.text.get("1.0", "end-1c"))#original line
        #workaround to fix the "umlaut" problem:
        try:
            chars = str(self.text.get("1.0", "end-1c"))
        except:
            c = self.text.get("1.0", "end-1c")
            chars = ''
            for i in c:
                j = ord(i)
                k = chr(j)
                chars = chars + k
            chars = str(chars)
        #end of workaround
        try:
            f = open(filename, "w")
            f.write(chars)
            f.close()
            ## print "saved to", `filename`
            return 1
        except IOError, msg:
            tkMessageBox.showerror("I/O Error", str(msg),
                                   master=self.text)
            return 0

This may take a few seconds if you want to save a large file, but at
least it is better as if it wasn't possible at all.

Any hints on this would be very appreciated.
Thanks in anticipation.

Michael




More information about the Python-list mailing list