簡體   English   中英

用numpy的genfromtxt讀取每第n行的最快方法

[英]Fastest way to read every n-th row with numpy's genfromtxt

我用numpy的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-th 6-th n-th )行。 根據numpy.genfromtxt.html ,沒有參數可以做到這一點。 我不想迭代數組。 有沒有推薦的方法來處理這個問題?

為了避免讀取整個數組,您可以將np.genfromtxtitertools.islice組合以跳過行。 這比讀取整個陣列然后切片要快一些(至少對於我嘗試過的較小陣列)。

例如,這是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)

返回一個數組x036的上面的文件的索引的元素:

array([12, 17, 62])

無論如何你必須閱讀整個文件,選擇第n個元素做類似的事情:

>>> a = np.arange(50)
>>> a[::5]
array([ 0,  5, 10, 15, 20, 25, 30, 35, 40, 45])

如果您只想在最終輸出文件中使用特定行,那么為什么不保存那些行而不是保存整個“測量”矩陣:

 output_rows = [5,7,11] np.savetxt('measurementProfileFormatted.txt',measurement[output_rows,:]) 

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM