escape backslash in encoded data

Question:

I have created a script which contains serial communication and DB connect for request.
retrieve data from com port is ok, exec procedure also works fine.
I have problem after data from DB are received. I need to send it to com port as an answer.

Answer from script is "00000011D88Ax1b[2JSome text answerx1b[1qx1b[1qx0d".
before it could be send to com port, the string has to be casted to bytearray.
So I use .encode(). But then the string changed to: "00000011D88A\x1b[2JSome text answer\x1b[1q\x1b[1q\x0d".

How I have to cast the string to not change the meaning? I know that "" is an escape character. But how to omit to use it, when the device need it?

Asked By: Urbis

||

Answers:

x1b[2J is special code which terminal should use to change something in terminal (ie. change color, change cursor position, etc.) and print() shouldn’t display it.

If you see x1b then you have string with escaped \x1b which is treads as three chars/bytes \ , 1, b instead of one char/byte with hex code 1B

You may also check what you get with print(...) and print(repr(...))

To convert string \x1b to single char/byte you need encode()/decode() with special encoding 'unicode_escape' (and/or 'raw_unicode_escape')

See Python documentation Codecs – Text Encodings

print('--- before ---')
text = "00000011D88A\x1b[2JSome text answer\x1b[1q\x1b[1q\x0d"
print('str  :', text)
print('repr :', repr(text))
print('bytes:', text.encode())

print('--- after ---')
text = text.encode().decode('unicode_escape')
print('str  :', text)
print('repr :', repr(text))
print('bytes:', text.encode())

Result:

--- before ---
str  : 00000011D88Ax1b[2JSome text answerx1b[1qx1b[1qx0d
repr : '00000011D88A\x1b[2JSome text answer\x1b[1q\x1b[1q\x0d'
bytes: b'00000011D88A\x1b[2JSome text answer\x1b[1q\x1b[1q\x0d'
--- after ---
str  : 00000011D88ASome text answer
repr : '00000011D88Ax1b[2JSome text answerx1b[1qx1b[1qr'
bytes: b'00000011D88Ax1b[2JSome text answerx1b[1qx1b[1qr'
Answered By: furas
Categories: questions Tags:
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.