1

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?

1
  • Unless you've a reason to avoid having numpy as a dependency, google numpy.loadtxt Commented Apr 29, 2013 at 0:27

1 Answer 1

3

Try numpy.

Below is one way of doing it.

import numpy as np
f = open(filename,"r")
N = np.fromfile(fp,dtype=np.int32,count=2)
a = np.fromfile(fp,dtype=np.float64)
a = np.resize(a,N)

with this you can also read a mixed format/type( text + binary ) file. It is possible to combine line 3 and line 4 by properly formatting the dtype option, google for more examples.

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.