I need to convert a binary file 'file.bin' to a numpy file 'file.npy', I hope that you could help me because I don't find any solution.
-
Does this help ?docs.scipy.org/doc/numpy/reference/generated/…Tony Tannous– Tony Tannous2017-02-10 10:22:49 +00:00Commented Feb 10, 2017 at 10:22
-
I have already seen that , but it doesn't give the binary type.nass9801– nass98012017-02-10 10:31:53 +00:00Commented Feb 10, 2017 at 10:31
-
1If you do not know the data type it is impossible to extract the data.Jan Christoph Terasa– Jan Christoph Terasa2017-02-10 10:59:06 +00:00Commented Feb 10, 2017 at 10:59
-
Possible duplicate of convert a text of binary values to numpy fileChanda Korat– Chanda Korat2017-02-10 11:35:26 +00:00Commented Feb 10, 2017 at 11:35
-
No, It isn't a duplicate question because I am talking about converting all the binary file to another numpy file.nass9801– nass98012017-02-10 12:29:02 +00:00Commented Feb 10, 2017 at 12:29
Add a comment
|
1 Answer
You can load the file with np.fromfile into an array and then np.save this array. You can specify the structure of the binary file using the dtype which can be a struct too.
import numpy as np
arr = np.fromfile('file.bin', dtype=np.float64)
np.save('file.npy', arr)
# optional to delete old file
import os
os.remove('file.bin')
np.memmap is helpful too, if it is a large file or you want to specify an offset.