0
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

Sathy
  • 303
  • 2
  • 8
  • 18
  • Given that `flask` output, what should the result be? `PROCESS1`? `PROCESS1 : ...` until the end of the string? Something else? Also, do you have any information on the `PROCESS1` string? If it is the name of a process you know then it might be possible to use a regex to find it, without trimming the start of the text. – Bakuriu Sep 19 '13 at 09:43
  • The result should be PROCESS1 : till the end of the valid text for the user. No, I will not be aware that PROCESS1 will be the output. It might be any.. – Sathy Sep 19 '13 at 12:43

2 Answers2

2

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 ".

Bakuriu
  • 98,325
  • 22
  • 197
  • 231
  • Can you help with your regex to remove the `"` as well – Sathy Sep 20 '13 at 05:53
  • You mean the last `"`? I believe it would be easier to simply do `regex.sub('', output)[:-1]` and remove the last character with slicing. Otherwise you have to change the regex to use a group for the content in the middle, *assuming* the `PROCESS1...` string *cannot* contain the `"`. – Bakuriu Sep 20 '13 at 06:09
-1

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"
vinit_ivar
  • 610
  • 6
  • 16