简体   繁体   中英

Python extraction of date data from a binary file

I have a file that I open in binary format using

with open(filename, 'br') as f2

I then need to extract certain blocks of Hex. There will be lots of these 'dates' in the code that will look like:

F2 96 E6 20 36 1B E4 40

I need to extract every instance of this in order for me to complete my date editing on it. Each 'date' will end with hex char 40 as above.

I have tried regex but these do not seem to work as I want.

For example

re.findall(b'............\\\x40', filename)

Can anyone assist?

I think your are confusing bytes with hex representation. 0x40 is a hexadecimal representation of the integer 64 and it's ascii code of the symbol @ .

with open(filename, 'rb') as f:
    results = re.findall('.{7}@', f.read())
    print results

Please note, that following regexps are equivalent: '.{7}@' , '.......@' , '.......\\x40'

>>> print 0x40, hex(64)
64 0x40
>>> print chr(0x40)
@

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM