1

I apologize that I just start to learn Python, and I searched and did not find much what I need.

In C++ I would do something like

float* data = new float[bytes];
std::ifstream b_stream("MyFile.bin", std::fstream::in | std::fstream::binary);
b_stream.read((char*)data, bytes);

I am wondering how would I something similar in Python? I am using Eclipse + Python 3.5. Basically I need to load data into my buffer in the format of float.

Thanks a lot.

Here is what I tried:

def read_into_buffer(filename):
    buf = bytearray(os.path.getsize(filename))
    with open(filename, 'rb') as f:
        f.readinto(buf)
    return buf

fileName = 'C:\\truth.bin';
buf = read_into_buffer(fileName)

but it seems buf is not in the format of float, or I don't know how to see it as float.

Here is another one I tried:

fileName = 'C:\\truth.bin';
with open(fileName) as f:
    floats = map(float, f)

Again, I don't know how to see floats, so I don't know I have read it successfully.

I just want to load it into some sorta container so I can access it with some indexing. Thanks.

Nick X Tsui
  • 2,737
  • 6
  • 39
  • 73

0 Answers0