Printing characters outside of the ASCII range

Andrew Berg bahamutzero8825 at gmail.com
Fri Nov 9 12:39:50 EST 2012


On 2012.11.09 11:17, danielk wrote:
> I'm converting an application to Python 3. The app works fine on Python 2.
> 
> Simply put, this simple one-liner:
> 
> print(chr(254))
> 
> errors out with:
> 
> Traceback (most recent call last):
>   File "D:\home\python\tst.py", line 1, in <module>
>     print(chr(254))
>   File "C:\Python33\lib\encodings\cp437.py", line 19, in encode
>     return codecs.charmap_encode(input,self.errors,encoding_map)[0]
> UnicodeEncodeError: 'charmap' codec can't encode character '\xfe' in position 0: character maps to <undefined>
> 
> I'm using this character as a delimiter in my application.
> 
> What do I have to do to convert this string so that it does not error out?
> 
That character is outside of cp437 - the default terminal encoding on
many Windows systems. You will either need to change the code page to
something that supports the character (if you're going to change it, you
might as well change it to cp65001 since you are using 3.3), catch the
error and replace the character with something that is in the current
codepage (don't assume cp437; it is not the default everywhere), or use
a different character completely. If it works on Python 2, it's probably
changing the character automatically to a replacement character or you
were using IDLE, which is graphical and is not subject to the weird
encoding system of terminals.
-- 
CPython 3.3.0 | Windows NT 6.1.7601.17835



More information about the Python-list mailing list