0

I am looking for a way of putting non-printable characters in a string in python.

Small background information: I am working with windows command prompt over SSH using paramiko. however paramiko does not 'parse'/'work with' the ANSI Terminal Emulation escape sequences. So they are appearing in my output, which I do not want. So I need to strip-off these escape sequences.

These strings/escape sequences however are encoded in the following manner: Esc[Line;ColumnH

And as you can guess. I can't get the esc-byte value in a string. It will either print as, well, something like 0x1b or 27. Thus useless.

Is there anyone who knows how to solve this? Thanks in advance.

3
  • Escape the non-ascii characters (by replacing with literals), do regex magic, un-escape? Commented Mar 5, 2012 at 11:56
  • with many applications, setting environment variable TERM=dumb will prevent them from sending ANSI escape sequences. Commented Mar 5, 2012 at 12:35
  • @PauloScardine sadly, this does not seem to work. Commented Mar 6, 2012 at 9:01

1 Answer 1

1

You can suppress these escape sequences using regex :

str_with_ainsi_esc = '\x1b[10;10HSalut'
print re.sub('\x1b\[\d+;\d+H','',str_with_ainsi_esc)
Sign up to request clarification or add additional context in comments.

2 Comments

Ahh, so that is how you put byte-chars in a string. Thanks. Will take a closer look tomorrow. (home-time now)
I did change '\x1b\[\d+;\d+H' to '\x1b\[\d*;\d*H' because both the column and row value are optional according to the VT100 spec.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.