Search code examples
pythonnumpyfilebinaryendianness

How to open big endian encoding (ieee-be) with python?


I have this matlab code that opens a .eeg file:

f_in=fopen(a_eegfile,'r','ieee-be');% open file where eeg data are with Big-endian encoding

It gives me a 1D matrix of doubles. (793456x1)

I am trying to do the same thing with python and numpy:

data_f = np.fromfile(os.path.join(root,folder,filename), dtype='>f8')

It works but I'm not getting the same matrix at all. Probably a problem in the dtype argument but I can't find it.

Anyone could help?

See:

https://numpy.org/doc/stable/reference/arrays.dtypes.html#arrays-dtypes-constru

https://fr.mathworks.com/help/matlab/ref/fopen.html#btrnibn-1-machinefmt


Solution

  • So in the end it was way eaiser than this:

    with open(os.path.join(root,folder,filename),'rb') as file:
        data = file.read()
        data_f = np.array([x for x in data])
    

    data_f now stores my (793456x1) matrix of integers.

    print(type(data_f),len(data_f), data_f.dtype)
    

    gives me:

    <class 'numpy.ndarray'> 16695840 int32

    with np as numpy.

    Hope this is Useful for someone.