0

I have a file where I have saved C float arrays as binary data. Is it possible to load this binary data into a Python list now?

6
  • 1
    docs.scipy.org/doc/numpy/reference/generated/… Commented Nov 8, 2019 at 18:52
  • Thank you. Sorry for my ignorance. I did try to search in various ways and formats 'how to read bindary array data from a file' but came out empty-handed Commented Nov 8, 2019 at 18:57
  • What, exactly do you mean by a "python array?" Commented Nov 8, 2019 at 18:58
  • What I mean is.. say we have a C array: float arr[] We save that array into a file as binary data Now in Python, we open this file, and read the binary data into an array my_array = array_from_file() Commented Nov 8, 2019 at 19:09
  • @JackAvante that doesn't answer my question at all. What exactly do you mean by array in Python? Do you mean an array from the array module? Or do you mean a numpy.ndarray? Or perhaps you just meant a Python list? Generally, "array" in Python means a numpy.ndarray or people from some other language incorrectly calling a list an array. Rarely, but possibly, they mean array.array. So, which one do you mean? Commented Nov 8, 2019 at 19:18

1 Answer 1

4

It's possible to do that using Numpy.memmap. Something like this:

import numpy as np
arr = np.memmap("filename", dtype="int32", mode="r")

Replace "filename" with the path to your array file and "int32" with the type you used in your C array.

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.