简体   繁体   中英

Bizarre behavior of python printing non-alphabetic ASCII characters

I have the following Python code:

for num in range(80, 150):
    input()
    print(num)
    print(chr(27))
    print(chr(num))

The input() statement is only there to control how quickly the for loop proceeds. I am not expecting this to do anything special, but when the loop hits certain numbers, printing that ASCII character, preceded by ASCII 27 (which is the ESC character) does some unexpected things:

  • At 92 and 94, the number does not print. http://i.stack.imgur.com/DzUew.png

  • At 99 (the letter c), a bunch of terminal output gets deleted. http://i.stack.imgur.com/5XPy3.png

  • At 108 (the letter l), the current line jumps up several lines (but text remains below). (didn't get a proper screencap, I'll add one later if it helps)

  • At 128 or 129, the first character starts getting masked. You have to type something (I typed "jjj") in order to prevent this from happening on that line. http://i.stack.imgur.com/DRwTm.png

I don't know why any of this happens although I imagine it has something to do with the ESC character interacting with the terminal. Could someone help me figure this out?

Esc with those characters make a special code for terminal .

A terminal control code is a special sequence of characters that is printed (like any other text). If the terminal understands the code, it won't display the character-sequence, but will perform some action. You can print the codes with a simple echo command.

Terminal Codes

For example,

  • ESC / = ST, String Terminator (chr(92))
  • ESC ^ = PM, Privacy Message (chr(94)) .

Control Sequences are different based on what terminal do you use.

More about:

It is due to confusion between escape sequences and character-encoding.

Your program is printing escape sequences , including

  • escape c (resets the terminal)
  • escape ^ (begins a privacy message, which causes other characters to be eaten)

In ISO-8859-1 (and ECMA-48), character bytes between 128 and 159 are considered control characters , referred to as C1 controls. Several of these are treated the same as escape combined with another character. The mapping between C1 and "another character" is not straightforward, but the interesting ones include

  • 0x9a which is device attributes , causing characters to be sent to the host.
  • 0x9b which is control sequence initiator , more usually seen as escape [ .

On the other hand, bytes in the 128-159 range are legal parts of a UTF-8 character. If your terminal is not properly configured to match the locale settings, you can find that your terminal responds to control sequences.

OSX terminal implements (does not document...) many of the standard control sequences. XTerm documents these (and many others), so you may find the following useful:

For amusement, you are referred to the xterm FAQ: Interesting but misleading

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM