1

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
Yousuf Memon
  • 4,638
  • 12
  • 41
  • 57

2 Answers2

1

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

\x80 (128) is not included there.

falsetru
  • 357,413
  • 63
  • 732
  • 636
  • @YousufMemon, Which ASCII chart? – falsetru Jan 14 '14 at 14:56
  • @YousufMemon: Where'd you find this chart? Whatever it's showing isn't ASCII. – Wooble Jan 14 '14 at 14:56
  • It's extended ASCII: http://www.theasciicode.com.ar/ascii-control-characters/form-feed-female-symbol-venus-ascii-code-12.html – Thorsten Kranz Jan 14 '14 at 14:57
  • So this function ``chr()`` only encodes Standard ASCII not Extended ASCII – Yousuf Memon Jan 14 '14 at 14:58
  • ``The argument must be in the range [0..255], inclusive`` taken from Python official documentation under ``chr()`` function heading. Link http://docs.python.org/2/library/functions.html#chr It looks it should support extended ASCII – Yousuf Memon Jan 14 '14 at 15:02
  • @Wooble It's extended ASCII – Yousuf Memon Jan 14 '14 at 15:03
  • @YousufMemon, You're seeing wrong documentation (Python 2.x). See http://docs.python.org/3/library/functions.html#chr (Python 3.x). In python 3.x, the argument is Unicode codepoint. – falsetru Jan 14 '14 at 15:03
  • @falsetru oops I should always google with Python version number. – Yousuf Memon Jan 14 '14 at 15:15
  • 1
    Note that there are about 100 different "extended ASCII" encodings which is why this wholly ambiguous phrase should be entirely avoided. The table which was linked in an earlier comment likely represents CP850, which is used by MS-DOS and is almost completely irrelevant nowadays. – ntoskrnl Jan 14 '14 at 17:42
1

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

Thorsten Kranz
  • 12,492
  • 2
  • 39
  • 56