[PyQt] Qstring and encode
Kyle Altendorf
sda at fstab.net
Mon Sep 25 12:42:42 BST 2017
On 2017-09-25 01:06, Phil wrote:
> The following code fails under python's IDLE, yet works from the Eric
> IDE. Under Eric "mytext" comes from a LineEdit box as explained in my
> previous message.
Perhaps they are running different copies of Python?
print(sys.executable)
print(sys.version)
> mytext = "Fred"
> byte = bytes(mytext)
> byte = byte + '\n'
> #ser.write(b mytext)
> ser.write(byte)
>
>
> Traceback (most recent call last):
> File "/home/phil/Python/serial_test.py", line 11, in <module>
> byte = bytes(mytext)
> TypeError: string argument without an encoding
This is bytes complaining about you not giving it an encoding.
byte = bytes(mytext, encoding='ascii')
https://docs.python.org/3/library/stdtypes.html#bytes
And make sure you use the encoding expected by the device on the other
end of the serial connection. If it expects ascii and you use utf-8
you may send it unexpected bytes and cause it trouble.
> This block of code also works under Eric but fails from the command
> line because QString does not have an encode attribute.
>
> mytext = mytext + "\n"
> mybytes = mytext.encode('ascii')#'utf-8')
> ser.write(mybytes)
Always include full tracebacks instead of "doesn't work".
Cheers,
-kyle
More information about the PyQt
mailing list