最快读取具有numpy的的genfromtxt每第n行的方式 [英] Fastest way to read every n-th row with numpy's genfromtxt

查看:754
本文介绍了最快读取具有numpy的的genfromtxt每第n行的方式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我读我的数据与numpy的的genfromtxt:

I read my data with numpy's genfromtxt:

import numpy as np
measurement = np.genfromtxt('measurementProfile2.txt', delimiter=None, dtype=None, skip_header=4, skip_footer=2, usecols=(3,0,2))
rows, columns = np.shape(measurement)
x=np.zeros((rows, 1), dtype=measurement.dtype)
x[:]=394
measurement = np.hstack((measurement, x))
np.savetxt('measurementProfileFormatted.txt',measurement)

这工作正常。但我想永远只能 5个 6个(所以 n个)排在最后的输出文件。
numpy.genfromtxt.html 没有参数这将做到这一点。我不想遍历数组。有没有办法解决这个问题一个推荐的方法?

this works fine. But i want only ever 5-th, 6-th (so n-th) row in the final Output file. According to numpy.genfromtxt.html there is no Parameter which would do that. I dont want to iterate the array. Is there a recommended way to deal with this problem?

推荐答案

要避免读取整个阵列可以结合 np.genfromtxt 和itertools .islice 来跳过行。这是略高于读取整个数组,然后切片(至少在更小的数组我试过)更快。

To avoid reading the whole array you can combine np.genfromtxt with itertools.islice to skip the rows. This is marginally faster than reading the whole array and then slicing (at least for the smaller arrays I tried).

例如,这里的内容 file.txt的

12
34
22
17
41
28
62
71

再例如:

>>> import itertools
>>> with open('file.txt') as f_in:
        x = np.genfromtxt(itertools.islice(f_in, 0, None, 3), dtype=int)

返回数组 X 0 3 6 以上文件的索引的元素:

returns an array x with the 0, 3 and 6 indexed elements of the above file:

array([12, 17, 62])

这篇关于最快读取具有numpy的的genfromtxt每第n行的方式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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