Say we have a simple, small file with a 1D array holding different value types, with a specific structure (first item is MATLAB uint
, second value is MATLAB uint
, and the rest of values are float
)
How can I read such an array of heterogeneous types from a file in Python?
The equivalent code in MATLAB is below.
function M = load_float_matrix(fileName)
fid = fopen(fileName);
if fid < 0
error(['Error during opening the file ' fileName]);
end
rows = fread(fid, 1, 'uint');
cols = fread(fid, 1, 'uint');
data = fread(fid, inf, 'float');
M = reshape(data, [cols rows])';
fclose(fid);
end
Note: this thread describes the following approach to read three consecutive uint32
values:
f = open(...)
import array
a = array.array("L") # L is the typecode for uint32
a.fromfile(f, 3)
but, how do I know that L is the typecode for uint32
? What about the other types? (e.g. float
).
Also, how can I read consecutive values from f
? Would a.fromfile
move the read pointer in the file forward?