简体   繁体   English

将numpy数组追加到文本文件中

[英]Appending numpy arrays into a text file

I have a function which returns a 2 Dimensional numpy array and this function will be inside a loop. 我有一个返回二维numpy数组的函数,该函数将在循环内。 At each iteration, I want to append this numpy array into a file. 在每次迭代时,我都希望将此numpy数组附加到文件中。

filename = "xyz"
for i in range(10):
      np_array = function_to_get_numpy()
      now append this `np_array` into filename

I can continue to append the numpy array in the script and dump once, but I want to avoid that. 我可以继续在脚本中追加numpy数组并转储一次,但我想避免这种情况。

Also I would prefer to store this in non-binary format. 我也希望以非二进制格式存储它。

In [64]: with open('xyz','w') as f:
    ...:     for n in range(1,4):
    ...:         arr = np.arange(n*n).reshape(n,n)
    ...:         np.savetxt(f, arr, fmt='%5d', delimiter=',')
    ...:         
In [65]: cat xyz
    0
    0,    1
    2,    3
    0,    1,    2
    3,    4,    5
    6,    7,    8

If the number of columns varies, as it does here, it will be hard(er) to read. 如果列数变化(如此处所做的那样),将很难读取。 csv readers like genfromtxt won't like it. genfromtxt这样的csv阅读器将不喜欢它。

If the number of columns is consistent, it can be loaded as one big array. 如果列数一致,则可以将其作为一个大数组加载。 Separating the writes and reloading them is possible, but more involved. 分离写入并重新装入它们是可能的,但涉及更多。

I'm going to take the opportunity to plug nppretty , a pretty printer for numpy that I've been working on. 我将借此机会插入nppretty ,这是我一直在努力的用于numpy的漂亮打印机。 It provides a class ArrayStream that I think will do exactly what you need. 它提供了一个ArrayStream类,我认为它将完全满足您的需求。

Install nppretty with: 使用以下命令安装nppretty

pip install nppretty

You can use ArrayStream much like a file object. 您可以像使用文件对象一样使用ArrayStream For example, this code: 例如,此代码:

from nppretty import ArrayStream
import numpy as np

arrstr = ArrayStream('arraystream.txt', name='arr2D')
for i in range(10):
    arr = np.arange(10*i, 10*(i + 1))
    arrstr.write(arr.reshape(2,5))
arrstr.close()

will produce a text file called arraystream.txt with the following contents: 将产生一个名为arraystream.txt的文本文件,其内容如下:

arr2D = [
[0, 1, 2, 3, 4],
[5, 6, 7, 8, 9],
[10, 11, 12, 13, 14],
[15, 16, 17, 18, 19],
[20, 21, 22, 23, 24],
[25, 26, 27, 28, 29],
[30, 31, 32, 33, 34],
[35, 36, 37, 38, 39],
[40, 41, 42, 43, 44],
[45, 46, 47, 48, 49],
[50, 51, 52, 53, 54],
[55, 56, 57, 58, 59],
[60, 61, 62, 63, 64],
[65, 66, 67, 68, 69],
[70, 71, 72, 73, 74],
[75, 76, 77, 78, 79],
[80, 81, 82, 83, 84],
[85, 86, 87, 88, 89],
[90, 91, 92, 93, 94],
[95, 96, 97, 98, 99],
]

Notes on ArrayStream 关于ArrayStream注意事项

ArrayStream accepts all of the same arguments as the standard Python open method. ArrayStream接受与标准Python open方法相同的所有参数。 The one additional keyword arg is name , which sets the name of the array in the file ( name defaults to "array" if left blank). 另外一个关键字arg是name ,它设置文件中数组的名称(如果保留空白,则name默认为"array" )。 Just like the file object returned by a call to open , an ArrayStream instance can used in a with statement. 就像调用open返回的文件对象一样, ArrayStream实例可以在with语句中使用。 For example, the following code will produce the same output as above: 例如,以下代码将产生与上面相同的输出:

with ArrayStream('arraystream.txt', name='arr2D') as f:
    for i in range(10):
        arr = np.arange(10*i, 10*(i + 1))
        f.write(arr.reshape(2,5))

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

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