繁体   English   中英

`with open`多个`findall`但一次打开文件

[英]`with open` multiple `findall` but open file once

我正在使用一些python代码来解析此.h文件:

#include <limits.h>

// Offsets
// UINT_MAX == 4294967295 (Note: do not remove the comments, they are used by parser.py)
#define ID_OFFSET           UINT_MAX - (1 << 6)     // ID_OFFSET        == 4294967231
#define CALL_NR_OFFSET      ID_OFFSET - (1 << 20)   // CALL_NR_OFFSET   == 4293918655

// Range of lower 2^20 numbers is reserved for debug statements.
#define DEBUG_OFFSET        (2 << 20)               // DEBUG_OFFSET == 1048576

// IDs for jpeg library functions
#define INIT_HEADER_VLD_ID  ID_OFFSET + 1
#define HEADER_VLD_ID       ID_OFFSET + 2
#define IQZZ_ID             ID_OFFSET + 3
#define IDCT_ID             ID_OFFSET + 4
#define CC_ID               ID_OFFSET + 5
#define RASTER_ID           ID_OFFSET + 6

// Core frequencies
#define MB1_FREQ            2.5 // in Mhz
#define MB2_FREQ            2.5 // in Mhz
#define MB3_FREQ            3   // in Mhz
#define MB4_FREQ            3   // in Mhz

我有多个这样的正则表达式:

uint_max_re = re.compile('UINT_MAX\s+=+\s+(\d+)\s+') # https://regexr.com/3pmk5
id_offset_re = re.compile('ID_OFFSET\s+=+\s+(\d+)') # https://regexr.com/3pmn8
call_nr_offset_re = re.compile('CALL_NR_OFFSET\s+=+\s+(\d+)') # https://regexr.com/3pmnh
debug_offset_re = re.compile('DEBUG_OFFSET\s+=+\s+(\d+)') # https://regexr.com/3pmnk
function_ids_re = re.compile('#define\s+(\w+)\s+ID_OFFSET\s+\+\s+(\d)') # https://regexr.com/3pmkt
core_freqs_re = re.compile('#define MB(\d+)_FREQ\s+(\d[.\d]*)') # https://regexr.com/3pml6

def parse_wrapper_params():
    "This parses the wrapper_params.h file and returns a dictionary with function_id as key and function_name as value"
    with open(wrapper_params_file, 'r') as f:
        uint_max = int(uint_max_re.findall(f.read())[0])
    with open(wrapper_params_file, 'r') as f:
        id_offset = int(id_offset_re.findall(f.read())[0])
    with open(wrapper_params_file, 'r') as f:
        call_nr_offset = int(call_nr_offset_re.findall(f.read())[0])
    with open(wrapper_params_file, 'r') as f:
        debug_offset = int(debug_offset_re.findall(f.read())[0])
    with open(wrapper_params_file, 'r') as f:
        function_ids = function_ids_re.findall(f.read())
    with open(wrapper_params_file, 'r') as f:
        core_freqs = core_freqs_re.findall(f.read())
    return {
        'uint_max': uint_max,
        'id_offset': id_offset,
        'call_nr_offset': call_nr_offset,
        'debug_offset': debug_offset,
        'function_ids': {int(id_offset) + int(function_id): function_name.lower() for function_name, function_id in function_ids},
        'core_freqs': {int(core): freq for core, freq in core_freqs}
    }

现在这很慢,所以我正在寻找一种方法来仅用with open(wrapper_params_file, 'r') as f:的单个文件执行此操作,但是如果删除第二个,则正则表达式将找不到任何内容。

任何帮助表示赞赏。 谢谢。

一次将内容读入变量。 另外,由于只需要第一个匹配项,因此请使用.findall() .search()而不是.findall()

with open(wrapper_params_file, 'r') as f:
    contents = f.read()
uint_max = uint_max_re.search(contents).group(1)
id_offset = id_offset_re.search(contents).group(1)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM