How to deal hex 0d and 0a in strings. scripting?

We’re capturing scanner values. The length (using len()) of the scanned value is a bit longer than what is expected. Loading the value in a hex editor reveals there is a 0d 0a at the end of each scanned value. A line feed and carriage return.

Can anyone tell me a reliable way (in scripting) to remove 0d and 0a from a string?
And also how to append 0d and 0a to a string?

value[:-2] would chop off the last 2 characters.

value + '\r\n' should add the CRLF back.

1 Like

I want to be certain that I’m only targeting CRs and LFs so I’m doing something like (below) . . Which seems to work perfectly

val = val.replace(’\r’, ‘’)
val = val.replace(’\n’, ‘’)

How about rstrip()?

2021-12-03 at 3.33 PM

5 Likes

make it

from os import linesep
value + linesep

just to make sure nothing funky happens.
I’ve had issues with this in the past, so now I’m very careful with this :X

1 Like