This is how I reproduce the problem:
Create a log file called "temp.log" and paste this line into it
DEBUG: packetReceived '\ x61 \ x62 \ x63'
I want to have a script that will read a line from a log file and decode a binary string ('\ x61 \ x62 \ x63'). For decoding, I use struct, therefore:
struct.unpack ('BBB', '\ x61 \ x62 \ x63')
Gotta give me
(97, 98, 99)
Here is the script I'm using
import re
import struct
import sys
f = open (sys.argv [1], 'r')
for line in f:
print line
packet = re.compile (r "packetReceived \ '(. *) \'"). search (line) .group (1)
# packet is the string r '\ x61 \ x62 \ x63'
assert (len (packet), 12)
# this works ok (returns (97, 98, 99))
struct.unpack ('BBB', '\ x61 \ x62 \ x63')
# this fails because packet is interpreted as r '\\ x61 \\ x62 \ x63'
struct.unpack ('BBB', packet)
I am running a script using temp.log as a script argument.
Hope the comments emphasize my problem. How can I make a variable package be interpreted as "\ x61 \ x62 \ x63"?
ASIDE: when I first edited this question, I assumed that reading the line from the file was the same: line = "DEBUG: packageReceived '\ x61 \ x62 \ x63'" which made the package == 'abc'
however it is actually the same as this one (using rawstring) line = r "DEBUG: packageReceived '\ x61 \ x62 \ x63'"
source
share