python - 在读取文件 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),直到找到我需要的标题?
我试图使用迭代器来做到这一点,不幸的是没有任何成功。这是完成任务的正确方法吗?
如果可能的话,我不想将数据保存到内存中。
解决方案
这是跳过您不关心的行的一般方法:
for line in file:
if 'somepattern' not in line:
continue
# if we got here, 'somepattern' is in the line, so process it
推荐阅读
- python - 如何将自定义包中的模块正确导入 AWS Lambda 函数?
- haskell - 从haskell中的char和int [([Char], Int)]列表中输出一个字符串列表([String])
- c - 如何将时间输入到结构中?
- html - 仅悬停覆盖图像上的 Bootstrap 4 卡
- django - 如何提高 PDF 文件(Django)的渲染时间?
- javascript - 将提交的表单转换为表单数据对象
- pandas - 如何使用 PIL 从 pandas 数据框中的行值创建图像?
- javascript - 如何减小图片的大小并将其变成视差效果?
- python - 如何将电报频道中的消息保存为变量
- python - 试图从 MySQL 获取数据