如何只读取文本文件中的特定行? [英] How to read only specific rows from a text file?

查看:81
本文介绍了如何只读取文本文件中的特定行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试处理文本文件中存储的数据,该文本文件如下所示:test.dat:

I am trying to process data stored in a text file which looks like this test.dat:

-1411.85  2.6888   -2.09945   -0.495947   0.835799   0.215353   0.695579   
-1411.72  2.82683   -0.135555   0.928033   -0.196493   -0.183131   -0.865999   
-1412.53  0.379297   -1.00048   -0.654541   -0.0906588   0.401206   0.44239   
-1409.59  -0.0794765   -2.68794   -0.84847   0.931357   -0.31156   0.552622   
-1401.63  -0.0235102   -1.05206   0.065747   -0.106863   -0.177157   -0.549252   
....
....

但是该文件只有几个GB,我非常想以小块的行读取它.我想使用numpy's loadtxt函数,因为这会将所有内容快速转换为numpy array.但是,到目前为止,我还无法进行管理,因为该功能似乎只提供了如下所示的列:

The file however is several GB and I would very much like to read it in, in small blocks of rows. I would like to use numpy's loadtxt function as this converts everything quickly to a numpy array. However, I have not been able to manage so far as the function seems to only offer a selection of columns like here:

data = np.loadtxt("test.dat", delimiter='  ', skiprows=1, usecols=range(1,7))

任何想法如何实现这一目标?如果loadtxt无法使用Python中的任何其他选项?

Any ideas how to achieve this? If it is not possible with loadtxt any other options available in Python?

推荐答案

hpaulj在他的评论中向我指出了正确的方向.

hpaulj pointed me in the right direction in his comment.

使用以下代码非常适合我:

Using the following code works perfectly for me:

import numpy as np
import itertools
with open('test.dat') as f_in:
    x = np.genfromtxt(itertools.islice(f_in, 1, 12, None), dtype=float)
    print x[0,:]

非常感谢!

这篇关于如何只读取文本文件中的特定行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆