Question Using self.process.readAll() gives 'b"', '\n' and '\r'

salm2s

Honorable
Jul 21, 2017
266
6
10,815
Hi there,

So I was making a test gui (PyQt5) that could read text from a command and then show it on the gui.

However, for some reason, it keeps showing this random b", \n and \r:
IUWngIX.png


I have tried using the readLine().decode('utf-8'), but everytime i do that it crashes. Plus, when i implement a new command to run, that command will continue to run and I want to show all of it.

So my question is: Is there a way to get rid of all the b', \n and \r in the readAll function.

My code:

Code:
output = self.process.readAll()
self.plainTextEdit.setPlainText(str(output))

Thanks,
salm2s
 
Not sure about the full requirements.

And I only see two lines of code....

Overall, it appears that quite a bit is missing.

The idea being is that if you implement a command to run (however that is implemented(?) then the gui will appear showing that command in the Window title and the results of the command within the window.

Tutorial link that should help you:

https://pythonbasics.org/pyqt-hello-world/

Provide more explanation and show all of your code.
 
Not sure about the full requirements.

And I only see two lines of code....

Overall, it appears that quite a bit is missing.

The idea being is that if you implement a command to run (however that is implemented(?) then the gui will appear showing that command in the Window title and the results of the command within the window.

Tutorial link that should help you:

https://pythonbasics.org/pyqt-hello-world/

Provide more explanation and show all of your code.


My Full code:
Code:
# -*- coding: utf-8 -*-

# Form implementation generated from reading ui file 'untitled.ui'
#
# Created by: PyQt5 UI code generator 5.13.1
#
# WARNING! All changes made in this file will be lost!


from PyQt5 import QtCore, QtGui, QtWidgets


class Ui_Form(object):
    def setupUi(self, Form):
        Form.setObjectName("Form")
        Form.resize(400, 300)
        self.pushButton = QtWidgets.QPushButton(Form)
        self.pushButton.setGeometry(QtCore.QRect(150, 240, 75, 23))
        self.pushButton.setObjectName("pushButton")
        self.plainTextEdit = QtWidgets.QPlainTextEdit(Form)
        self.plainTextEdit.setGeometry(QtCore.QRect(0, 0, 401, 241))
        self.plainTextEdit.setObjectName("plainTextEdit")

        self.process = QtCore.QProcess(Form)
        self.process.readyRead.connect(self.read1)

        self.pushButton.clicked.connect(self.hello_World)

        self.retranslateUi(Form)
        QtCore.QMetaObject.connectSlotsByName(Form)

    def retranslateUi(self, Form):
        _translate = QtCore.QCoreApplication.translate
        Form.setWindowTitle(_translate("Form", "Form"))
        self.pushButton.setText(_translate("Form", "Run"))

    def hello_World(self):
        self.process.start('test1.bat')

    def read1(self):
        read1 = self.process.readAll()
        output1 = str(read1)
        self.plainTextEdit.setPlainText(output1)




if __name__ == "__main__":
    import sys
    app = QtWidgets.QApplication(sys.argv)
    Form = QtWidgets.QWidget()
    ui = Ui_Form()
    ui.setupUi(Form)
    Form.show()
    sys.exit(app.exec_())

What i wanted to do was to get the 'hello world' only, not with all the b" and \n and \r
 
So everything else is okay except for the b" and \n and \r - correct?

Where do you think those letters are coming from?

Are you able to describe step by step what the code is to do?

What is test1.bat doing (or supposed to do) and what output is expected and subsequently expected to appear (I think) in the "Hello World" box?
 
So everything else is okay except for the b" and \n and \r - correct?

Where do you think those letters are coming from?

Are you able to describe step by step what the code is to do?

What is test1.bat doing (or supposed to do) and what output is expected and subsequently expected to appear (I think) in the "Hello World" box?
The test1.bat executes a 'echo' command. I think that the b" \n and \r are functions in which python uses to format text (like \n is new line). That's why i am asking is there a way to reformat it so that it doesnt show these functions. I think it is automatically encoding it into utf-8.
 
Fair enough.

You will probably need an extra "step" to take the results of the 'echo' command and strip out the unwanted characters.

Fortunately, Python is very powerful with respect to string functions and methods.

Caveat being that the input to any given string function is consistent or, if not, you can apply some logic to address and handle any inconsistencies.

Reference for string functions/methods:

https://towardsdatascience.com/useful-string-methods-in-python-5047ea4d3f90

https://www.journaldev.com/24588/python-string-functions

You can easily find other similar links.

Best of all, you may have multiple ways to accomplish the same objective when it comes to string manipulations.

E.g.: You can take the leftmost characters, the rightmost characters, or grab the middle characters.

In many cases, a single function will do the job. However, in more complex situations, you may need to apply some progressive combination of functions.

What to do:


As a separate bit of code take the entire echoed output text/line from test1.bat and assign the value to a variable.

Next put that variable through whatever string function(s) seem applicable to you and will parse the variable as needed.

No need to keep running through the existing code. Just code and test independently....

When you find a function that strips out b" \n and \r and leaves only "Hello World" then that is the string function to use.

Then incorporate the string function into your existing code and test.
 
I think i found a solution:
So i used the readAll() function, but i used to .replace command. So:
Code:
output1 = str(read1).replace('b"', '').replace('\n', '').replace('\r', '')
Sorry for not updating solution in a while, forgot about this thread