[英]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
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.
每个“日期”将以上述的十六进制字符40结尾。
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 @
. 0x40
是整数64
的十六进制表示形式,它是符号@
的ascii代码。
with open(filename, 'rb') as f:
results = re.findall('.{7}@', f.read())
print results
Please note, that following regexps are equivalent: '.{7}@'
, '.......@'
, '.......\\x40'
请注意,以下正则表达式是等效的:
'.{7}@'
, '.......@'
, '.......\\x40'
>>> print 0x40, hex(64)
64 0x40
>>> print chr(0x40)
@
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.