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.