python - Python 读取文件时跳过几行

标签 python file iterator readfile skip

我有一个由曲线数据组成的文件,其重复结构如下:

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。

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

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

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

最佳答案

这是跳过您不关心的行的通用方法:

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

关于python - Python 读取文件时跳过几行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53134027/

相关文章:

python - 如何使用 for 循环替换字符串中的多个字符?

java - 天数 : tmap Nullpointer exception while merging two CSV files

c# - 视觉 C# : Move multiple files with the same extensions into another directory

C++:使用迭代器调用 Vector::erase

c++ - 遍历特征矩阵的最有效方法

python - 使用 `K.constant` 或 `self.add_weight(trainable=False)` 作为层中的固定权重有什么不同

python - 将文件夹中的所有文件名更改为其他Python名称

python - 为什么我会得到这个 AttributeError?

c - GDB 输入文件等

c++ - 不在 city.begin() 开始 map 迭代器