How to capture all characters in a binary string without python interpretation

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'"

+3
source share
4

, , '\x61\x62\x63'. 12 , 15, 3 .

, ipython (, , ), python repr(), , . Python, repr() , Python.

:

for char in packet:
    print("%5d %2s %2r" % (ord(char), char, char))

, . , , repr .

:

struct.unpack('BBB', packet)

struct.unpack('BBB', packet.decode('string_escape'))
+1

Python , . , , , line. , :

line = r"DEBUG: packetReceived '\x61\x62\x63'"
print re.compile(r"packetReceived '(.*)'").search(line).group(1)

\x61\x62\x63.

+5
>>> re.compile(r"packetReceived '(.*)'").search(r"DEBUG: packetReceived '\x61\x62\x63'").group(1)
'\\x61\\x62\\x63'

No, this line is not where your problem is.

+2
source

If you are sure that you are getting twelve characters, and not just three represented as twelve, it might just be printing a line that makes you sad.

For comparison:

>> print '\x61\x62\x63'
abc
>>> print r'\x61\x62\x63'
\x61\x62\x63

My 50c on you actually gets three characters, and they print as follows:

>>> print ''.join('\\x%02x' % ord(c) for c in 'abc')
\x61\x62\x63
+1
source

All Articles