0

subprocess.popen is returning the output as class bytes as below

b'Caption FreeSpace Size \r\r\nC: 807194624
63869808640 \r\r\nD: \r\r\nY:
216847310848 2748779065344 \r\r\n\r\r\n'

How to remove all occurance of \r\r\n Or how can I convert this to python string or array

3 Answers 3

2
my_str = "hello world"

Converting string to bytes

my_str_as_bytes = str.encode(my_str)

Converting bytes to string

my_decoded_str = my_str_as_bytes.decode()
Sign up to request clarification or add additional context in comments.

Comments

2

You can print it by:

print(b'Caption FreeSpace ... \r\r\n\r\r\n'.decode())    

Result:

Caption FreeSpace ...

1 Comment

Also with string = XXX.decode() you can convert it to string and treat it as a string, such as split or anything
1

You can remove the characters with translate like so

b'Caption FreeSpace ... \r\r\n\r\r\n'.translate(None, b'\r\n')

which results in

b'Caption FreeSpace Size C: 807194624 63869808640 D: Y: 216847310848 2748779065344 '  

If you know the encoding of the returned data you may want to use decode which will give you a string for further processing.
For example, assumed it is encoded in utf-8, you can just call decode with its default value and directly call split on it to split by white-space characters to get an array like this

b'Caption FreeSpace ... \r\r\n\r\r\n'.translate(None, b'\r\n').decode().split()  

Result

['Caption', 'FreeSpace', 'Size', 'C:', '807194624', '63869808640', 'D:', 'Y:', '216847310848', '2748779065344']

2 Comments

Thanks for info. When the type is < class list>.then how can i replace all \r\n. Because when i apply decode it says .testresult is <class 'list'> type Encountered exception during remote execution print (testresult.decode()) AttributeError: 'NoneType' object has no attribute 'decode'
Sry for late reply ... Can you show a bit more of your code so that I can see how testresult is being filled/set?

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.