You have less
configured to display raw characters as ASCII character combinations representing these non-printable characters; the -r
or --raw-control-chars
command line switch does this:
Causes "raw" control characters to be displayed. The default is to display control characters using the caret notation; for example, a control-A (octal 001) is displayed as "^A". Warning: when the -r option is used, less cannot keep track of the actual appearance of the screen (since this depends on how the screen responds to each type of control character). Thus, various display problems may result, such as long lines being split in the wrong place.
This is a special less
feature.
If you want to do the same with your Python program, you'll need to make the same translation yourself. Create a mapping turning 'special' characters into escape codes:
nonprintable = {
'\x00': '^@',
'\x01': '^A',
'\x02': '^B',
'\x03': '^C',
'\x04': '^D',
# etc.
}
for i in range(128):
character = chr(i)
print i, nonprintable.get(character, character)