Printing characters outside of the ASCII range
Ian Kelly
ian.g.kelly at gmail.com
Fri Nov 9 12:34:51 EST 2012
On Fri, Nov 9, 2012 at 10:17 AM, danielk <danielkleinad at gmail.com> 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?
In Python 2, chr(254) means the byte 254.
In Python 3, chr(254) means the Unicode character with code point 254,
which is "þ". This character does not exist in CP 437, so it fails to
encode it for output.
If what you really want is the byte, then use b'\xfe' or bytes([254]) instead.
More information about the Python-list
mailing list