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

asked Sep 19, 2013 at 8:24
2
  • 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. Commented Sep 19, 2013 at 9: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.. Commented Sep 19, 2013 at 12:43

2 Answers 2

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

answered Sep 19, 2013 at 13:02
Sign up to request clarification or add additional context in comments.

2 Comments

Can you help with your regex to remove the " as well
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 ".
-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"
answered Sep 19, 2013 at 8:31

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.