将列表拆分为无重复项的文件

2024-09-28 05:23:34 发布

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

像这样的大数据文件:

133621    652.4   496.7  1993.0 ...
END       SAMPLES EVENTS  RES  271.0     2215.0 ...
ESACC     935.6   270.6  2215.0 ...
115133    936.7   270.3  2216.0 ...
115137    936.4   270.4  2219.0 ...
115141    936.1   271.0  2220.0 ...
ESACC L   114837    115141  308   938.5   273.3    2200
115145    936.3   271.8  2220.0 ...
END 115146  SAMPLES EVENTS  RES   44.11   44.09
SFIX L   133477
133477    650.8   500.0  2013.0 ...
133481    650.2   499.9  2012.0 ...
ESACC     650.0   500.0  2009.0 ...

我们只想把ESACC数据抓取到试验中。当END出现时,前面的ESACC数据被聚合到一个试验中。现在,我可以将ESACC数据的第一个块放入一个文件中,但是由于循环从数据的开头重新启动,所以它一直只获取第一个块,所以我有80个完全相同的数据的试验。你知道吗

for i in range(num_trials):
   with open(fid) as testFile:
       for tline in testFile:

           if 'END' in tline:
               fid_temp_start.close()
               fid_temp_end.close()   #Close the files
               break

           elif 'ESACC' in tline:

               tline_snap = tline.split()
               sac_x_start = tline_snap[4]
               sac_y_start = tline_snap[5

               sac_x_end = tline_snap[7]
               sac_y_end = tline_snap[8]

我的问题是:如何迭代到下一个数据块而不获取前一个数据块?你知道吗


Tags: 数据inforreseventsstartendsamples
1条回答
网友
1楼 · 发布于 2024-09-28 05:23:34

尝试重写代码,如下所示:

def data_parse(filepath): #Make it a function
    try:
        with open(filepath) as testFile:
            tline = '' #Initialize tline
            while True: #Switch to an infinite while loop (I'll explain why)
                while 'ESACC' not in tline: #Skip lines until one containing 'ESACC' is found
                    tline = next(testFile)  #(since it seems like you're doing that anyway)

                tline_snap = tline.split()
                trial = [tline_snap[4],'','',''] #Initialize list and assign first value
                trial[1] = tline_snap[5]

                trial[2] = tline_snap[7]
                trial[3] = tline_snap[8]

                while 'END' not in tline:  #Again, seems like you're skipping lines
                    tline = next(testFile) #so I'll do the same

                yield trial #Output list, save function state

    except StopIteration:
        fid_temp_start.close() #I don't know where these enter the picture
        fid_temp_end.close()   #but you closed them so I will too
        testfile.close()

#Now, initialize a new list and call the function:
trials = list()
for trial in data_parse(fid);
    trials.append(trial) #Creates a list of lists

这就产生了一个生成函数。通过使用yield而不是return,函数返回一个值并保存其状态。下次调用该函数时(正如您将在最后的for循环中重复所做的那样),它会从停止的地方重新开始。它从最近执行的yield语句后面的行开始(在本例中,该语句重新启动while循环),重要的是,它记住任何变量的值(如tline的值和它在数据文件中停止的点)。你知道吗

当您到达文件的末尾(并记录了所有的试验)时,tline = next(testFile)的下一次执行将引发StopIteration错误。try - except结构捕获该错误并使用它退出while循环并关闭文件。这就是为什么我们使用无限循环;我们希望继续循环,直到错误迫使我们退出。你知道吗

最后,您的数据以列表列表的形式存储在trials中,其中每个项等于[sac_x_start, sac_y_start, sac_x_end, sac_y_end],正如您在代码中定义的那样,用于一次试验。你知道吗

注意:在我看来,当代码不包含ESACC或END时,它似乎完全跳过了行。我已经复制了,但我不确定这是否是你想要的。如果您想得到中间的行,只需将其添加到'END'循环中即可重写,如下所示:

while 'END' not in tline:
    tline = next(testFile)
    #(put assignment operations to be applied to each line here)

当然,您必须相应地调整用于存储此数据的变量。你知道吗

编辑:天哪,我刚才注意到这个问题有多老了。你知道吗

相关问题 更多 >

    热门问题