简体   繁体   English

将文本文件分成多个numpy数组

[英]Break a text file into multiple numpy arrays

I want to take a simple, single column file containing floats into multiple numpy arrays that are each created from the same number of lines. 我想将一个包含浮点数的简单单列文件放入多个numpy数组中,每个数组都使用相同数量的行创建。

So, for example, if the file has 180 lines, I'd like to create 3 numpy arrays, one containing data from lines 1-60, 2nd from 61 to 120, and 3rd from 121 to 180. 因此,例如,如果文件有180行,我想创建3个numpy数组,一个包含第1-60行的数据,第二个从61到120,第三个从121到180。

I was attempting to use the np.fromfile() function, but this doesn't seem to be able to respond to file pointers (as in, seek to 1st line, read 60 lines, then seek to 61, read 60, ...). 我试图使用np.fromfile()函数,但这似乎无法响应文件指针(如,寻求第一行,读取60行,然后寻求61,读取60,.. )。 Does anyone know how this can be done efficiently? 有谁知道如何有效地完成这项工作?

arr1, arr2, arr3 = np.loadtxt(path).reshape(3,-1)

Just read all the data into an array, then reshape the array to have 3 rows. 只需将所有数据读入数组,然后将数组重新整形为3行。 The -1 in the call to reshape will be replaced by reshape by whatever number makes sense. reshape调用中的-1将被reshape的任意数字取代。 For example, if the array has length 180, then upon reshaping, the array will have 60 columns. 例如,如果数组的长度为180,那么在重新整形时,该数组将具有60列。 Note that the length of the original array must be exactly divisible by 3. 请注意,原始数组的长度必须完全可被3整除。

Alternatively, 或者,

arr1, arr2, arr3 = np.array_split(np.loadtxt(path), 3)

This is a bit more robust since array_split will split the array into 3 parts even if the length of the original array is not exactly divisible by 3. 这更加健壮,因为即使原始数组的长度不能被3整除, array_split也会将数组拆分为3个部分。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM