如何从这个文件中提取数据?

时间:2015-09-01 05:12:41

标签: python regex

我有一个包含以下内容的文本文件:

Block 15, type A, key 05a3e664414a :00  00  00  00  00  00  ff  07  80  69  94  82  c8  cc  46  0a  
Block 14, type A, key 05a3e664414a :14  14  14  18  10  18  13  13  10  13  18  19  11  10  11  15  
Block 13, type A, key 05a3e664414a :15  14  14  14  18  10  18  13  13  10  13  18  19  11  10  11  
Block 12, type A, key 05a3e664414a :07  05  04  04  04  08  00  08  03  03  10  13  18  19  11  10  
Block 11, type A, key 05a398f21b26 :00  00  00  00  00  00  ff  07  80  69  4c  36  e4  30  46  0a  
Block 10, type A, key 05a398f21b26 :08  00  07  05  04  07  04  08  00  08  13  13  10  13  18  18  
Block 09, type A, key 05a398f21b26 :05  08  00  07  05  06  04  04  08  00  18  13  13  10  13  18  
Block 08, type A, key 05a398f21b26 :11  15  18  10  17  15  14  14  14  18  10  18  13  13  10  13  
Block 07, type A, key 0577a23d1e96 :00  00  00  00  00  00  ff  07  80  69  2c  3c  7a  44  ee  0a  
Block 06, type A, key 0577a23d1e96 :03  10  11  15  18  10  17  15  14  14  14  18  10  18  13  07  
Block 05, type A, key 0577a23d1e96 :09  07  10  11  15  18  10  17  15  14  14  14  18  10  18  13  
Block 04, type A, key 0577a23d1e96 :18  19  11  10  11  15  18  10  17  15  14  14  14  18  10  18  
Block 03, type A, key a64ca2e58cc1 :00  00  00  00  00  00  ff  07  80  69  a6  4c  a2  e5  8c  c1  
Block 02, type A, key a64ca2e58cc1 :00  00  00  00  00  00  00  00  00  00  00  00  00  00  00  00  
Block 01, type A, key a64ca2e58cc1 :70  60  62  67  7a  50  42  55  65  59  42  5c  15  14  14  14  
Block 00, type A, key a64ca2e58cc1 :c4  ed  60  47  0e  08  04  00  62  63  64  65  66  67  68  69 

我想编写一个python程序来提取密钥并按如下方式返回:

[[0xa6, 0x4c, 0xa2, 0xe5, 0x8c, 0xc1],
[0x05, 0x77, 0xa2, 0x3d, 0x1e, 0x96],
[0x05, 0xa3, 0x98, 0xf2, 0x1b, 0x26],
[0x05, 0xa3, 0xe6, 0x64, 0x41, 0x4a],]

这等于:

[[hex form of block 00 key],
[hex form of block 04 key],
[hex form of block 08 key],
[hex form of block 12 key]]

我该怎么做?我是说如何检测使用 regex 的模式?

2 个答案:

答案 0 :(得分:3)

你可以在没有正则表达式的情况下完成

s="Block 15, type A, key 05a3e664414a :00  00  00  00  00  00  ff  07  80  69  94  82"

key = s.split()[5] # gets the key = 05a3e664414a

print ["0x%s" % key[i:i+2] for i in range(0, len(key), 2)]

# output ['0x05', '0xa3', '0xe6', '0x64', '0x41', '0x4a']

答案 1 :(得分:2)

part1 = (binascii.unhexlify(match) for match in re.findall("key ([a-fA-F0-9]+)",my_text))

part2 = [struct.unpack("B"*len(x),x) for x in part1]

print "Repr1:",part2
print "Repr2:",[map(hex,x) for x in part2]
print "Repr3:",[map("0x{0:02x}".format,x) for x in part2]