4

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!

Lautaro
  • 93
  • 2
  • 9
  • 1
    I'm pretty sure `file.write()` expects a string or buffer. `v.shape` is a tuple, and passing it as an argument will raise a `TypeError`. – John Vinyard Nov 08 '12 at 14:46

2 Answers2

5

You can use numpy.save() which saves in binary.

Bitwise
  • 7,577
  • 6
  • 33
  • 50
  • 3
    Here's the .npy spec (https://github.com/numpy/numpy/blob/master/doc/neps/npy-format.txt), so the data can be interpreted from the C++ code after calling `save()`. – John Vinyard Nov 08 '12 at 14:44
  • But I need to read first the number of rows and columns from the C++ program. It doesn't save that info, does it? – Lautaro Nov 08 '12 at 16:42
  • Oh, didn't see that. Anyway, thanks, but I already solved it in another way. – Lautaro Nov 08 '12 at 17:26
2

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))
mgilson
  • 300,191
  • 65
  • 633
  • 696