0

How can one perform the equivalent of the chr() function for any arbitrary encoding? Consider this attempt (doesn't work):

for i in range(128, 255):
    print("%s " % (i.encode('cp1252'),) )

This fails with AttributeError: 'int' object has no attribute 'encode'.

Neither does this attempt work:

for i in range(128, 255):
    j = "\x%x " % (i,)
    print("%x " % (j.encode('cp1252'),) )

This fails with SyntaxError: (unicode error) 'unicodeescape' codec can't decode bytes in position 0-2: truncated \xXX escape.

I am specifically targeting Python 3.

Ray
  • 2,472
  • 18
  • 22
dotancohen
  • 30,064
  • 36
  • 138
  • 197

2 Answers2

2

What exactly are you trying to achieve? The second attempt comes down to this:

for i in range(128, 255):
    j = chr(i)
    print("%x " % (j.encode('cp1252'),) )

But I don't see how that's the equivalent of chr for cp1252. chr(i) converts from the position i in the code page to the corresponding character (the return value is a unicode string of length 1). You can do a similar conversion for cp1252 (or any other encoding), using bytes([i]).decode('cp1252'), which looks more like your first attempt.

raymonad
  • 939
  • 1
  • 5
  • 9
  • I had to decode `j` and print it as a string, but this worked. `print("%s " % (j.encode('cp1252').decode('cp1252'), ))`. Note also that code points 128-159 would not work, I had to start at 160. Thank you. – dotancohen Feb 09 '14 at 10:56
1

In Python 3, I believe you want this,

chr(i).encode('cp1252')

or in Python 2.x, you would use,

unichr(i).encode('cp1252')

Full code is,

for i in range(128, 255):
    print("%s " % (unichr(i).encode('cp1252',errors='replace')))

On my console, the cp1252 encoding does not work and throws an exception. To at least print a hex value, you can include the errors part.

This post is relevant: Python 3: Demystifying encode and decode methods and How to print non-ascii characters to file in Python 2.7

Community
  • 1
  • 1
William Denman
  • 3,046
  • 32
  • 34