Question

print proc1
"\u001b[H\u001b[2J\r\nPRINT ME"

How to print only "PRINT ME". I do not want to print other chunks.

The above is an output of a shell script when Python fabric gives a run call. I return the fabric message to my Flask as

return json.dumps(proc1)

Update:

The response from flask is as below:

"Output": "\"\\u001b[H\\u001b[2J\\r\\nPROCESS1       : process not running\\r\\n \\r\\nPROCESS2       : process running\\r\\nT\""}

I'm looking at removing \u001b[H2J and other junks

Was it helpful?

Solution

The simplest way I can think of is to use str.split and its maxsplit parameter:

In [1]: output = "\"\\u001b[H\\u001b[2J\\r\\nPROCESS1       : process not running\\r\\n \\r\\nPROCESS2       : process running\\r\\nT\""

In [2]: output.split('\\r\\n', maxsplit=1)[-1]
Out[2]: 'PROCESS1       : process not running\\r\\n \\r\\nPROCESS2       : process running\\r\\nT"'

If you are using an old version of python(I believe python2 and python3.x, x <= 2) you may need to specify the maxsplit as a positional parameter:

In [3]: output.split('\\r\\n', 1)[-1]
Out[3]: 'PROCESS1       : process not running\\r\\n \\r\\nPROCESS2       : process running\\r\\nT"'

The junk at the beginning of the output seems like an escape sequence that clears the terminal(at least, doing:

print "\u001b[H\u001b[2J\r\nPRINT ME".decode('unicode-escape')

Has this effect in the Konsole.

Assuming the format will always be "UTUT\r\n with U being a unicode escape in the form \uxxxx with x hexadecimal digits, and T being a terminal escape made of [ plus digits and letters, the following should be able to strip off the first characters:

In [9]: regex = re.compile(r'"\\u(\d|[a-f])+\[(\w|\d)+\\u(\d|[a-f])+\[(\d|\w)+\\r\\n')

In [10]: regex.sub('', output)
Out[10]: 'PROCESS1       : process not running\\r\\n \\r\\nPROCESS2       : process running\\r\\nT"'

If you don't want the last " you can simply do:

regex.sub('', output)[:-1]

Which will simply trim the beginning of the string and remove the last character. If for some reason the " may not be the last character you could use:

In [2]: regex = re.compile(r'"\\u(\d|[a-f])+\[(\w|\d)+\\u(\d|[a-f])+\[(\d|\w)+\\r\\n(?P<content>[^"]+)')

In [3]: output = "\"\\u001b[H\\u001b[2J\\r\\nPROCESS1       : process not running\\r\\n \\r\\nPROCESS2       : process running\\r\\nT\""

In [4]: regex.match(output).group('content')
Out[4]: 'PROCESS1       : process not running\\r\\n \\r\\nPROCESS2       : process running\\r\\nT'

Where I assume the string that you need does not contains ".

OTHER TIPS

Python is representing the string proc1 as a byte string. Make sure it's represented as a unicode string, prefix the string with a u.

So,

proc1 = u"\u001b[H\u001b[2J\r\nPRINT ME"
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top