繁体   English   中英

将列表拆分成没有重复的文件

[英]Splitting a list into a file without duplicates

大型数据文件如下:

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]

我的问题:如何在不获取前几个数据块的情况下迭代到下一个数据块?

尝试用以下方式重写代码:

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)

当然,您必须调整用于存储此数据的变量。

编辑:亲爱的上帝,我刚才注意到了这个问题的年龄。

暂无
暂无

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

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