Python 3中的双线循环搜索?

2024-09-24 22:25:43 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在使用Python3,需要从文件中提取数据。数据示例如下所示:

ENERGY_BOUNDS 
  1.964033E+07  1.733253E+07  1.491825E+07  1.384031E+07  1.161834E+07  1.000000E+07  8.187308E+06  6.703200E+06
  6.065307E+06  5.488116E+06  4.493290E+06  3.678794E+06  3.011942E+06  2.465970E+06  2.231302E+06  2.018965E+06
GAMMA_INTERFACE
     0
EIGENVALUE 
  1.219034E+00
N,2N
  1.191994E+00  1.535081E+00  1.543891E+00  1.413861E+00  1.181815E+00  6.174152E-01  1.302440E-02  0.000000E+00
  0.000000E+00  0.000000E+00  0.000000E+00  0.000000E+00  0.000000E+00  0.000000E+00  0.000000E+00  0.000000E+00
  0.000000E+00  0.000000E+00  0.000000E+00  0.000000E+00  0.000000E+00  0.000000E+00  0.000000E+00  0.000000E+00
MACRO      1
SIGABS 
 -3.826074E-03 -3.707513E-04  2.610351E-03  6.961084E-03  7.832982E-03  7.512567E-03  1.018417E-02  1.276596E-02
  9.148128E-03  8.828235E-03  8.527789E-03  7.514346E-03  7.544248E-03  7.801064E-03  7.724884E-03  7.047571E-03
  5.280749E-03  3.999751E-03  3.821688E-03  3.748186E-03  3.712753E-03  3.591795E-03  3.390300E-03  3.180354E-03
SIGTRAN 
  7.513455E-02  8.061355E-02  8.377954E-02  8.787775E-02  9.114071E-02  9.170817E-02  9.440786E-02  9.535947E-02
  1.010975E-01  1.035364E-01  1.160553E-01  1.290131E-01  1.197249E-01  1.151962E-01  1.298934E-01  1.375417E-01
  1.428861E-01  1.715100E-01  1.627465E-01  2.026621E-01  2.007540E-01  1.644982E-01  1.781501E-01  1.624188E-01

这一过程需要:

逐行搜索文件,直到找到起始关键字(本例中为宏)

在此之后,继续逐行搜索,直到找到特定标识符

将标识符后面的行中的每个值读入数组或列表

找到另一个标识符后停止读取

到目前为止,这就是我所拥有的。如果标识符是宏后的第一个值(例如,如果它是SIGABS),但不适用于任何其他值(例如,SIGTRAN),则代码工作正常。我的结果文件中可能有50个不同的标识符,所以我需要代码来一次选择一个

def read_data_from_file_macro(file_name, start_macro, identifier):
    with open(file_name, 'r') as read_obj:
        list_of_results = []
        # Read all lines in the file one by one
        for line in read_obj:
            # For each line, check if line contains the string
            if start_macro in line:
            # If MACRO is found, start looking for the identifier read the next line
                nextValue = next(read_obj)
                if identifier in nextValue:
                # If identifier is found read next line
                    nextValue = next(read_obj)
                    while(not nextValue.strip().isidentifier()): #keep on reading untill next identifier appears 
                        list_of_results.extend(nextValue.split())
                        nextValue = next(read_obj)
                        # Convert to float
                        for i in range(0, len(list_of_results)): 
                                list_of_results[i] = float(list_of_results[i])
    return(list_of_results)

Tags: oftheinobjreadline标识符start
1条回答
网友
1楼 · 发布于 2024-09-24 22:25:43

尝试以下方法。它将您的文件作为文本处理,将包含在start_标识符和end_标识符之间的部分分开,并通过一些工作返回一个浮动列表,该列表最终在您的\u结果列表中扩展(在调用函数之前必须先存在,因此您必须首先手动创建它)。您可以运行任何一对标识符。让我知道它是如何工作的

def read_data_from_file_macro(file_name, start_identifier, end_identifier):
    with open(file_name) as f:
    t=f.read()
    t=t[t.find('MACRO'):]
    t=t[t.find(start_identifier)+len(start_identifier):t.find(end_identifier)]
    t=t.replace('\n', '').split()
    t=[float(i) for i in t if not i.isidentifier()]
    list_of_results.extend(t)

相关问题 更多 >