将numpy数组的大小写入二进制文件(write numpy array with its size to binary file)

我需要将2D numpy数组写入文件,包括其尺寸,以便我可以从C ++程序中读取它并创建相应的数组。

我编写了一些保存数组的简单代码,可以从C ++中读取,但是如果我首先尝试编写数组的大小,它总会给我一个错误。

这是我简单的python代码:

1 file = open("V.bin","wb") 2 file.write(V.shape) 3 file.write(V) 4 file.close()

第二行给出错误,我也尝试过:

n1, n2 = V.shape file.write(n1) file.write(n2)

但它也不起作用。

我正在添加它显示的错误:

回溯(最近调用最后一次):file.write(V.shape [0])TypeError:必须是字符串或缓冲区,而不是int

谢谢!

I need to write a 2D numpy array to a file, including its dimensions so I can read it from a C++ program and create the corresponding array.

I have written some simple code that saves the array and it can be read from C++, but if I try to write the array's size first it always gives me an error.

Here's my simple python code:

1 file = open("V.bin","wb") 2 file.write(V.shape) 3 file.write(V) 4 file.close()

The second line gives the error, I've also tried:

n1, n2 = V.shape file.write(n1) file.write(n2)

But it doesn't work either.

I'm adding the error it shows:

Traceback (most recent call last): file.write(V.shape[0]) TypeError: must be string or buffer, not int

Thanks!

最满意答案

如果要将其保存为ascii,可以使用numpy.savetext 。

或者(因为看起来你正在处理二进制数据),如果要保存原始数据流,可以使用ndarray.tostring来获取可以直接转储到文件的字节字符串。

这种方法的优点是您可以创建自己的文件格式。 缺点是您需要创建一个字符串才能将其实际写入文件。


并且因为你说你在第二行遇到错误,所以这是一个错误,因为f.write需要一个字符串。 你试图传递一个tuple或者int 。 您可以使用struct.pack来解决此问题:

f.write(struct.pack('2i',*array.shape))

You can use numpy.savetext if you want to save it as ascii.

Alternatively (since it looks like you're dealing with binary data), if you want to save the raw data stream, you could use ndarray.tostring to get a string of bytes that you can dump to the file directly.

The advantage of this approach is that you can create your own file format. The downside is that you need to create a string in order to actually write it to the file.


And since you say that you're getting an error on the second line, it's an error because f.write expects a string. You're trying to pass it a tuple or ints. You could use struct.pack to solve this problem:

f.write(struct.pack('2i',*array.shape))将numpy数组的大小写入二进制文件(write numpy array with its size to binary file)

我需要将2D numpy数组写入文件,包括其尺寸,以便我可以从C ++程序中读取它并创建相应的数组。

我编写了一些保存数组的简单代码,可以从C ++中读取,但是如果我首先尝试编写数组的大小,它总会给我一个错误。

这是我简单的python代码:

1 file = open("V.bin","wb") 2 file.write(V.shape) 3 file.write(V) 4 file.close()

第二行给出错误,我也尝试过:

n1, n2 = V.shape file.write(n1) file.write(n2)

但它也不起作用。

我正在添加它显示的错误:

回溯(最近调用最后一次):file.write(V.shape [0])TypeError:必须是字符串或缓冲区,而不是int

谢谢!

I need to write a 2D numpy array to a file, including its dimensions so I can read it from a C++ program and create the corresponding array.

I have written some simple code that saves the array and it can be read from C++, but if I try to write the array's size first it always gives me an error.

Here's my simple python code:

1 file = open("V.bin","wb") 2 file.write(V.shape) 3 file.write(V) 4 file.close()

The second line gives the error, I've also tried:

n1, n2 = V.shape file.write(n1) file.write(n2)

But it doesn't work either.

I'm adding the error it shows:

Traceback (most recent call last): file.write(V.shape[0]) TypeError: must be string or buffer, not int

Thanks!

最满意答案

如果要将其保存为ascii,可以使用numpy.savetext 。

或者(因为看起来你正在处理二进制数据),如果要保存原始数据流,可以使用ndarray.tostring来获取可以直接转储到文件的字节字符串。

这种方法的优点是您可以创建自己的文件格式。 缺点是您需要创建一个字符串才能将其实际写入文件。


并且因为你说你在第二行遇到错误,所以这是一个错误,因为f.write需要一个字符串。 你试图传递一个tuple或者int 。 您可以使用struct.pack来解决此问题:

f.write(struct.pack('2i',*array.shape))

You can use numpy.savetext if you want to save it as ascii.

Alternatively (since it looks like you're dealing with binary data), if you want to save the raw data stream, you could use ndarray.tostring to get a string of bytes that you can dump to the file directly.

The advantage of this approach is that you can create your own file format. The downside is that you need to create a string in order to actually write it to the file.


And since you say that you're getting an error on the second line, it's an error because f.write expects a string. You're trying to pass it a tuple or ints. You could use struct.pack to solve this problem:

f.write(struct.pack('2i',*array.shape))