Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

TypeError: data type not understood in Python fromfile

I am trying to read a binary file using following command:

import numpy as np
fid = open(filename, 'rb')
ax = np.fromfile(fid, dtype=np.int32, count=1)

This command is working fine, However

ay = np.fromfile(fid, dtype=np.char, count=16)

gives an TypeError: data type not understood. Any idea how can i read it as character type?

like image 741
Sulabh Tiwari Avatar asked Aug 16 '26 09:08

Sulabh Tiwari


2 Answers

Your desired data type is non-existent, np.char actually is a module.

Take a look at the numpy datatypes, you could cover your byte representation using np.byte, which is a np.int8.

like image 73
Finwood Avatar answered Aug 17 '26 23:08

Finwood


You should use

ay = np.fromfile(fid, dtype=np.byte, count=16)

instead of

ay = np.fromfile(fid, dtype=np.char, count=16)

because numpy doesn't contain scalar type char. More about numpy data types you could see here. numpy.byte type corresponding to C char type.
If you want convert array of 16 binary digits to one int you can use following code:

aybin = np.fromfile(fid, dtype=np.char, count=16)
ay = int(("".join(str(d) for d in aybin)), 2)
like image 27
kvorobiev Avatar answered Aug 18 '26 00:08

kvorobiev



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!