本文介紹了無法打印正確解碼的 readAllStandardOutput的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!
問題描述
我有這段代碼可以將一個進程的所有輸出打印到一個文本字段中:
I have this code to print into a text field all the output from a process:
data = self.m_process.readAllStandardOutput()
s = str(data)
self.m_ui.b_renderOutput.append(s)
我在輸出中得到的是:
b''
b''
b''
b'
Starting "C:\Program Files'
b''
b'\Autodesk\Maya2018\bin\mayabatch.exe"
'
b'Initialized VP2.0 renderer {
'
我無法將其解碼并以正確的方式打印.我知道來自 readAllStandardOutput 的是 QByteArray
I'm not able to decode it and print it in the right way. I know that what comes from readAllStandardOutput is a QByteArray
推薦答案
如果要轉換 QByteArray 為字符串,首先使用 data 將其轉換為字節() 方法,然后是 decode() 將其轉換為字符串:
If you want to convert QByteArray to string, first convert it to bytes using the data() method, and then decode() to convert it to string:
data = self.m_process.readAllStandardOutput()
s = data.data().decode() # <---
self.m_ui.b_renderOutput.append(s)
另一種方法是將 QByteArray 轉換為 bytearray 然后使用 decode():
Another method is to convert the QByteArray to bytearray and then use decode():
data = self.m_process.readAllStandardOutput()
s = bytearray(data).decode() # <---
self.m_ui.b_renderOutput.append(s)
這篇關于無法打印正確解碼的 readAllStandardOutput的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!
【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!