Reading fixed width strings with numpy.fromfile

Viewed 27

I have a binary file, that has the structure:

int32, <8-byte string>, 
int32, <8-byte string>,
int32, <8-byte string>,
.....

I would like to read it using np.fromfile (rather than line-by-line), since it's a very big file. I can create a custom data type like this:

myDatatype = np.dtype([('mynumber', np.int32), ('mystring', np.byte, 8)])

and then read it using:

data = np.fromfile(f, myDatatype)

The disadvantage is, that now data has a byte-array instead of a string. Is there a way use np.fromfile, so that it directly loads into a string-type?

0 Answers
Related