【问题标题】:Searching a file for matches between two values and outputting search hits in Python在文件中搜索两个值之间的匹配项并在 Python 中输出搜索结果
【发布时间】:2013-05-03 13:53:18
【问题描述】:

我正在(尝试)编写一个程序,该程序在十六进制文件中搜索两个值之间的十六进制字符串的实例,例如。在 D4135B 和 D414AC 之间,在第一个值之间递增,直到达到第二个值 - D4135B、D4135C、D4135D 等。 我已经设法让它增加等,但这是我遇到问题的搜索部分。 这是我到目前为止的代码,它是从其他地方拼凑而成的,我需要让它以某种方式将所有搜索命中输出到输出文件(file_out) 我已经超出了我对 Python 理解的极限,我确信可能有一种更简单的方法可以做到这一点。如果有任何帮助,我将不胜感激。

def search_process(hx):   # searching for two binary strings
    global FLAG
while threeByteHexPlusOne != threeByteHex2: #Keep incrementing until second value reached
If Flag:
    if hx.find(threeByteHex2) != -1:
    FLAG = False #If threeByteHex = ThreeByteHexPlusOne, end search
    Print (“Reached the end of the search”,hx.find(threeByteHexPlusOne))
    Else:
        If hx.find(threeByteHexPlusOne) != -1:
        FLAG = True
    Return -1 #If no results found

if __name__ == '__main__':
    try:
        file_in = open(FILE_IN, "r")   #opening input file
        file_out = open(FILE_OUT, 'w') #opening output file
        hx_read = file_in.read  #read from input file 
        tmp = ''
        found = ''
        while hx_read:   #reading from file till file is empty
            hx_read = tmp + hx_read
            pos = search_process(hx_read)

            while pos != -1:
                hex_read = hx_read[pos:]

                if FLAG:
                    found = found + hx_read

                pos = search_process(hx_read)   
            tmp = bytes_read[]
            hx_read = file_in.read

        file_out.write(found)  #writing to output file

    except IOError:
        print('FILE NOT FOUND!!! Check your filename or directory/PATH')

【问题讨论】:

  • 那么......当你运行它时会发生什么?它会抛出异常吗?此外,缩进和大小写都搞砸了。

标签: python search python-3.x hex iteration


【解决方案1】:

这是一个程序,它一次从文件中查看 3 个字节的十六进制字符串,如果 3 字节的十六进制字符串在给定的十六进制范围之间,它会将其写入另一个文件。它利用generators 使从十六进制字符串中获取字节更清晰。

import base64
import sys

_usage_string = 'Usage: python {} <input_file> <output_file>'.format(sys.argv[0])

def _to_base_10_int(value):
    return int(value, 16)

def get_bytes(hex_str):
    # Two characters equals one byte
    for i in range(0, len(hex_str), 2):
        yield hex_str[i:i+2]

def get_three_byte_hexes(hex_str):
    bytes = get_bytes(hex_str)
    while True:
        try:
            three_byte_hex = next(bytes) + next(bytes) + next(bytes)
        except StopIteration:
            break
        yield three_byte_hex

def find_hexes_in_range(hex_str, lower_bound_hex, upper_bound_hex):
    lower_bound = _to_base_10_int(lower_bound_hex)
    upper_bound = _to_base_10_int(upper_bound_hex)
    found = []
    for three_byte_hex in get_three_byte_hexes(hex_str):
        hex_value = _to_base_10_int(three_byte_hex)
        if lower_bound <= hex_value < upper_bound:
            found.append(three_byte_hex)
    return found

if __name__ == "__main__":
    try:
        assert(len(sys.argv) == 3)
    except AssertionError:
        print _usage_string
        sys.exit(2)
    file_contents = open(sys.argv[1], 'rb').read()
    hex_str = base64.decodestring(file_contents).encode('hex')
    found = find_hexes_in_range(hex_str, 'D4135B', 'D414AC')
    print('Found:')
    print(found)
    if found:
        with open(sys.argv[2], 'wb') as fout:
            for _hex in found:
                fout.write(_hex)

查看更多关于生成器的信息here

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-12-21
    • 1970-01-01
    • 2019-04-10
    • 1970-01-01
    • 1970-01-01
    • 2018-10-14
    相关资源
    最近更新 更多