[PyQt] UnicodeDecodeError with output from Windows OS command

Valentin Valls valentin.valls at esrf.fr
Thu Nov 30 11:12:17 GMT 2017


Hi. Are you looking for:

stream = qt.QDataStream(byte_array, qt.QIODevice.ReadOnly)
my_string = stream.readQString()

and

stream = qt.QDataStream(byte_array, qt.QIODevice.WriteOnly)
stream.writeQString(my_string)

where byte_array is a QByteArray

In my experience it looks to be portable, you dont have to encode/decode
the string. You only have to play with unicode python strings.

Regards,


On 11/30/2017 12:02 PM, J Barchan wrote:
> D
> ​ear, helpful PyQt/Python experts,
>
>
> I have a problem, receiving a ​UnicodeDecodeError.  The full details
> are at
> https://forum.qt.io/topic/85493/unicodedecodeerror-with-output-from-windows-os-command,
> in combination with
> https://forum.qt.io/topic/85064/python3-pyqt5-x-qbytearray-to-string. 
> Here I will try to be brief to cover only the salient points.:
>
>  1. I am spawning a QProcess under Windows to run an arbitrary OS
>     command, and grab its output to display in a window.
>  2. I use QByteArray Process.readAllStandardOutput() to receive the
>     child's output in the parent.
>  3. I wish to display it in a dialog window, so I (believe I need to)
>     use QLineEdit.setPlainText(QString) to do that.
>  4. To achieve that I will need to convert a QByteArray to a QString.
>  5. Since neither of these types and their methods are available in
>     PyQt, I asked how to do that in
>     https://forum.qt.io/topic/85064/python3-pyqt5-x-qbytearray-to-string.
>  6. The answer I got from a PyQt expert, "jazzycamel", who visited
>     there (and told me to come to this forum in future, which is why I
>     am here now!), was that the Python/PyQt way to do this is to work
>     with its native bytes & str types respectively, and use str =
>     bytes.decode('utf-8') to "convert".
>
> While this has worked fine under Linux, when a user runs my Qt app
> under Windows and issues a perfectly normal robocopy command under a
> standard UK Windows with really nothing special/unusual going on with
> filenames, he gets:
>
>     Unhandled Exception:
>
>     'utf-8' codec can't decode byte 0x9c in position 32: invalid start
>     byte
>
>     <class 'UnicodeDecodeError'>
>     File "C:\HJinn\widgets\messageboxes.py", line 289, in
>     processReadyReadStandardOutput
>     output = output.data().decode('utf-8')
>
> I understand this to be a Python/PyQt problem.  I /believe/ if I used
> the native Qt/C++ calls for this --- which apparently I can't from
> PyQt --- there would be no issue.
>
> I have over the years written, say, Windows C programs using standard
> Windows SDK calls for this kind of "redirector".  I simply grab the
> output of a sub-process and throw it at whatever the native Windows
> SetTextEdit() function is, and all has always been fine.
>
> Note that I have /never/ had to guess/decode/convert bytes to some
> text encoding, and this has worked across all platforms forever.  So I
> really don't expect to have to do so now, unless there's something
> going on in Qt/PyQt which is fundamentally any different.
>
> So... can you please tell me how under Python/PyQt I can just display
> the output from an OS command (assuming "text-type" output, I don't
> expect arbitrary binary bytes) without the slightest chance of any
> kind of "I can't convert" Exception, please?
>
> Thank you in advance!
>
>
>
>
>
>
> _______________________________________________
> PyQt mailing list    PyQt at riverbankcomputing.com
> https://www.riverbankcomputing.com/mailman/listinfo/pyqt

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://www.riverbankcomputing.com/pipermail/pyqt/attachments/20171130/89e0dfd7/attachment-0001.html>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: valentin_valls.vcf
Type: text/x-vcard
Size: 293 bytes
Desc: not available
URL: <https://www.riverbankcomputing.com/pipermail/pyqt/attachments/20171130/89e0dfd7/attachment-0001.vcf>


More information about the PyQt mailing list