简体   繁体   English

如何在命令提示符中将 \r\n 显示为实际的换行符?

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

I have this Python code which goes like我有这个 Python 代码,就像

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

When running this Python code in the Command Prompt, I'm met with this output在命令提示符中运行此 Python 代码时,我遇到了此 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而不是上面的 output,我想在 \r \n 被替换为实际的新行的地方显示它,它看起来像

'80ee:0021
8086:1e31'

The result is in bytes.结果以字节为单位。 So you have to call decode method to convert the byte object to string object.所以你必须调用decode方法将字节object转换为字符串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.如果您使用的是 Python 3.7+,则可以将text=True传递给subprocess.check_output以获取字符串形式的结果。

Prior to 3.7 you can use universal_newlines=True .在 3.7 之前,您可以使用universal_newlines=True text is just an alias for universal_newlines . text只是universal_newlines的别名

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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