Non-ascii characters in an MSDOS window

Bartolomé Sintes Marco BartolomeSintes at ono.com
Fri Oct 18 01:30:42 EDT 2002


Thanks for your help. I have tried your program. I have copied it to a
sample.py file,
but when I run it (Ctrl+F5) I get the following error message

Traceback (most recent call last):
  File "C:\Mis documentos\Barto\02-03 Abastos\Python\Acentos\sample.py",
line 20, in ?
    print " On: Spanish accents: á é í ó ú"
  File "C:\Mis documentos\Barto\02-03 Abastos\Python\Acentos\sample.py",
line 14, in write
    self.so.write(s.decode('latin-1').encode('cp437'))
  File "C:\PYTHON22\lib\encodings\cp437.py", line 18, in encode
    return codecs.charmap_encode(input,errors,encoding_map)
UnicodeError: charmap encoding error: character maps to <undefined>

and Python seems broken because after this error, I can not even save the
file. Am I doing
something wrong?

Best regards,
Barto

"Bengt Richter" <bokr at oz.net> escribió en el mensaje
news:aonrjm$2ag$0 at 216.39.172.122...
> You could try something like this version of your sample.py:
> (it assumes your strings are Latin-1 encoded)
> --
> # sample.py
> sa =  "Spanish accents: á é í ó ú"  # som we can see sample.sa
interactively
> class L1to437:
>     import sys
>     def __init__(self):
>         self.so = L1to437.sys.stdout
>     def on(self):
>         L1to437.sys.stdout = self
>     def off(self):
>         L1to437.sys.stdout = self.so
>     def write(self, s):
>         self.so.write(s.decode('latin-1').encode('cp437'))
> so437 = L1to437()
>
> if __name__ == '__main__':
>     print "Before: Spanish accents: á é í ó ú"
>     so437.on()
>     print "    On: Spanish accents: á é í ó ú"
>     so437.off()
>     print "   Off: Spanish accents: á é í ó ú"
>     end = raw_input()
> --
>
> Running it:
> [19:20] C:\pywk\junk>sample.py
> Before: Spanish accents: ß T f = ·
>     On: Spanish accents: á é í ó ú
>    Off: Spanish accents: ß T f = ·
>
> Or you can import it, and use sample.so437.on() to turn on coding of
output for the DOS window:
>
>  >>> import sample
>  >>> print sample.sa
>  Spanish accents: ß T f = ·
>  >>> sample.so437.on()
>  >>> print sample.sa
>  Spanish accents: á é í ó ú
>  >>> sample.so437.off()
>  >>> print sample.sa
>  Spanish accents: ß T f = ·
>
> But Martin is the expert on that stuff. There may be a clean way to do it.
>
> Regards,
> Bengt Richter





More information about the Python-list mailing list