1

I am trying to search through a hex dump for matching hex strings, ie in testHexData.txt there might be-20F09FE520F09FE51CF09FE518F09FE514F09FE50000A0E10CF09FE508F09FE5CEFABEBA300B00007C000028700000284900A0E3CC0100EB080200EA8716A0E3020BA0E3010080E000D0A0E194499FE5000094E5000050E30200000AC20100EB044084E2F9FFFFEA10089FE5000090E5F00000E28716A0E30000 and in testHex.txt there would be strings that may or may not be in testHexData.txt, eg. 20F09FE、518F09FE、FGG7988H等。

我找到了以下代码,当要搜索的数据是列表格式时有效,即猪狗猫,但如果要搜索的数据是连续字符串,则无效。代码运行没有任何错误,但不会产生结果。我确信解决方案很简单,但我正在兜圈子试图找到它。我将不胜感激任何帮助。谢谢 :)

file1 = set(line.strip() for line in open(r'C:\Python27\testHexData.txt'))

file2 = set(line.strip() for line in open(r'C:\Python27\testHex.txt'))

for line in file1 & file2:

    if line:

        print line
4

1 回答 1

1

您要么需要将整个文件加载到内存中(如果它很大则不好)或将其放入卡盘中。我调整了下面的内容,将其分成 2000 个大小的部分。假设您只想输出文件中的模式

patterns = set(line.strip() for line in open(r'C:\Python27\testHex.txt'))
max_pattern_len = max([len(p) for p in patterns])
with open(r'C:\Python27\testHexData.txt') as hex_file:
    prev_segment_part = ''
    for segment in hex_file.read(2000):
         seg = prev_segment_part + segment
         for pattern in list(patterns):
            if pattern in seg:
                print "Found:", pattern
                patterns.remove(pattern)
         prev_segment_part = seg[-max_pattern_len:]
于 2013-05-07T16:26:31.960 回答