I have a binary file which is simple a list of signed 32 bit ieee754 floating point numbers. They are not separated by anything, and simply appear one after another until EOF.
How would I read from this file and interpret them correctly as floating point numbers?
I tried using read(4)
, but it automatically converts them to a string with ascii encoding.
I also tried using bytearray
but that only takes it in 1 byte开发者_JAVA技巧 at a time instead of 4 bytes at a time as I need.
struct.unpack('f', file.read(4))
You can also unpack several at once, which will be faster:
struct.unpack('f'*n, file.read(4*n))
Take a peek at struct.unpack. Something like the following might work...
f = struct.unpack('f', data_read)
import struct
(num,) = struct.unpack('f', f.read(4))
The fastest approach (in terms of performance) I found so far is numpy.fromfile
import numpy as np
class FloatReader:
def __init__(self, filename):
self.f = open(filename, "rb")
def read_floats(self, count : int):
return np.fromfile(self.f, dtype=np.float32, count=count, sep='')
This approach is much faster than struct.unpack
in terms of performance!
精彩评论