Вопрос

When I'm trying to print all ASCII chars in Python only 127 are getting printed and I'm getting an error with output after 127. I'm unable to understand what does that error mean.

Example:

t = list(range(0, 256))
for x in t:
    print(str(x) + ". " + chr(x))

Error:

Traceback (most recent call last):
  File "D:\Study\Pedia\Python Book\6. Functions\2. Built-in functions\ord_chr.py", line 6, in <module>
    print(str(x) + ". " + str(chr(x)))
  File "C:\Python33\lib\encodings\cp1252.py", line 19, in encode
    return codecs.charmap_encode(input,self.errors,encoding_table)[0]
UnicodeEncodeError: 'charmap' codec can't encode character '\x80' in position 5: character maps to <undefined>

Note: I can't show you the complete output because I can't copy complete raw output from by sublime output console. Don't know the real reason why it can't get copied.


enter image description here

Это было полезно?

Решение

ASCII includes definitions for 128 characters (0 to 127).

\x80 (128) is not included there.

Другие советы

When I try you code on Windows, Python 2.7, it works without exception. I had to take screenshots as copying the text simply ignored those extended ascii characters.

part 1 part 2 part 3

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top