首页 > 解决方案 > 在读取文件 Python 时跳过大块行

问题描述

我有一个文件,其中包含重复结构如下的曲线数据:

numbersofsamples
Title
     data
     data
     data
      ...

例如:

999numberofsamples
title crvTitle
             0.0            0.866423
    0.0001001073           0.6336382
    0.0002002157           0.1561626
    0.0003000172          -0.1542121
             ...                 ...
1001numberofsamples
title nextCrv
    0.000000e+00        0.000000e+00
    1.001073e-04        1.330026e+03
    2.002157e-04        3.737352e+03
    3.000172e-04        7.578963e+03
             ...                 ...

该文件由许多曲线组成,最大可达 2GB。

我的任务是通过跳过我不感兴趣的块(曲线)来查找和导出特定曲线。我知道曲线的长度(样本数),所以应该有办法跳转到下一个分隔符(例如 numberofsamples),直到找到我需要的标题?

我试图使用迭代器来做到这一点,不幸的是没有任何成功。这是完成任务的正确方法吗?

如果可能的话,我不想将数据保存到内存中。

标签: pythonfileiteratorreadfileskip

解决方案


这是跳过您不关心的行的一般方法:

for line in file:
    if 'somepattern' not in line:
        continue
    # if we got here, 'somepattern' is in the line, so process it

推荐阅读