像这样的大数据文件:
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]
我的问题是:如何迭代到下一个数据块而不获取前一个数据块?你知道吗
尝试重写代码,如下所示:
这就产生了一个生成函数。通过使用
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'
循环中即可重写,如下所示:当然,您必须相应地调整用于存储此数据的变量。你知道吗
编辑:天哪,我刚才注意到这个问题有多老了。你知道吗
相关问题 更多 >
编程相关推荐