简体   繁体   中英

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'

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

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))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM