1

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.

5
  • Does this help ?docs.scipy.org/doc/numpy/reference/generated/… Commented Feb 10, 2017 at 10:22
  • I have already seen that , but it doesn't give the binary type. Commented Feb 10, 2017 at 10:31
  • 1
    If you do not know the data type it is impossible to extract the data. Commented Feb 10, 2017 at 10:59
  • Possible duplicate of convert a text of binary values to numpy file Commented 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. Commented Feb 10, 2017 at 12:29

1 Answer 1

5

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.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.