Python `打开'multiple'findall',但只打开一次文件

Python `打开'multiple'findall',但只打开一次文件,python,regex,findall,Python,Regex,Findall,我正在使用一些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 #defi

我正在使用一些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
现在这相当慢,所以我正在寻找一种方法,只使用一个打开的
(包装参数文件,'r')作为f:
,但如果删除第二个,regex将什么也找不到


感谢您的帮助。谢谢。

将内容读入变量一次。另外,使用
.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)
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)