ITNoob
ITNoob

Reputation: 17

How to display \r\n as actual new line in command prompt?

I have this Python code which goes like

output = str(check_output(["./listdevs.exe", "-p"]))
print (output)

When running this Python code in the Command Prompt, I'm met with this output

b'80ee:0021\r\n8086:1e31\r\n'

Instead of the above output, I would like to display it where the \r \n is replaced with an actual new line where it would look like

'80ee:0021
8086:1e31'

Upvotes: 0

Views: 741

Answers (2)

Philip Wrage
Philip Wrage

Reputation: 1559

If you are using Python 3.7+, you can pass text=True to subprocess.check_output to obtain the results as a string.

Prior to 3.7 you can use universal_newlines=True. text is just an alias for universal_newlines.

output = str(subprocess.check_output(["./listdevs.exe", "-p"], text=True))

Upvotes: 0

user459872
user459872

Reputation: 24602

The result is in bytes. So you have to call decode method to convert the byte object to string object.

>>> print(output.decode("utf-8"))

Upvotes: 1

Related Questions