I have the need to store python str in a database to retrieve it and then apply a format() and encode() methods on it in order to shape my request frame an convert it into bytes and finally send() it through a socket.
MWE is like this:
fstr = '{slaveid:}{command:s}\x0d'
cstr = fstr.format(slaveid=chr(128+43), command='flags')
bstr = cstr.encode()
And produce the following output:
{slaveid:}{command:s}
«flags
b'\xc2\xabflags\r'
My problem occurs at the third line, char greater than 127 become two bytes when performing encode() method. Is suppose it is all about charset definition and because default encoding 'ascii' is limited to 127.
How should I define my encoding in order to get the following conversion:
b'\xabflags\r'
I am a little lost in front of charset tables.
bytearray(cstr)cstr.encode('latin-1')encode()is encoding the string as UTF-8 and\xc2\xabis the UTF-8 encoding for\u00ab(the«character). You could trycstr.encode(encoding='iso-8859-1')instead.