2015-09-01 197 views
-3

我有一個包含以下內容的文本文件:如何從此文件提取數據?

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]] 

我該怎麼做?我的意思是我該如何檢測使用正則表達式

+1

凡'0x'從何而來? –

+0

多數民衆贊成在十六進制數...我更加好奇,其中a6和4c等來自... –

+1

@AvinashRaj我補充說,我自己 – Abraham

回答

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'] 
+0

堅實的答案和初學者比我容易一點(+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]