If statement 'not working' despite conditions being true
currently going through a bin file full of hex data to process, however currently a match I'm using to try and find a group of 3 hex bytes in the file aren't working correctly. The values are identical however it is not printing my string I've currently got set to confirm that its a match, at present I'm trying to just match the first 3 bytes so we know it works etc. the code is as follows:
You escaped the escape sequences, so \\x00 is four bytes, the \ backslash, then the letter x followed by two 0 digits.
Remove the backslash escapes:
match1 = "\x00\n\x00"
Don't try to print this directly; terminals generally won't make nulls visible, so you just get an extra newline. Use the repr() function to produce debug output that looks just like a Python string so you can reproduce that value in your code or the interactive interpreter:
print ("match1 =", repr(match1))
This is also how the interactive interpreter shows you expression results (unless they produced None):
Next, if you are using Python 3, you'll still won't have a match because you opened the file in binary mode and are thus getting bytes objects, but your match1 variable is a str text sequence. If you want the two types to match you'll either have to convert (encode the text or decode the bytes), or make match1 a bytes object to start with:
match1 = b'\x00\n\x00'
The b prefix makes that a bytes literal.
Asked in February 2016Viewed 2,829 timesVoted 10Answered 2 times