[英]How to display \r\n as actual new line in command prompt?
我有這個 Python 代碼,就像
output = str(check_output(["./listdevs.exe", "-p"]))
print (output)
在命令提示符中運行此 Python 代碼時,我遇到了此 output
b'80ee:0021\r\n8086:1e31\r\n'
而不是上面的 output,我想在 \r \n 被替換為實際的新行的地方顯示它,它看起來像
'80ee:0021
8086:1e31'
結果以字節為單位。 所以你必須調用decode
方法將字節object轉換為字符串object。
>>> print(output.decode("utf-8"))
如果您使用的是 Python 3.7+,則可以將text=True
傳遞給subprocess.check_output
以獲取字符串形式的結果。
在 3.7 之前,您可以使用universal_newlines=True
。 text
只是universal_newlines
的別名。
output = str(subprocess.check_output(["./listdevs.exe", "-p"], text=True))
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.