Home > database >  Error reading a binary file into a numpy array
Error reading a binary file into a numpy array

Time:12-07

I'm trying to insert values in a .bin file to an array. However, at the first line I get the error message:

ValueError: could not convert string to float: b'\x00\x00\x00\x00\xdd\xb9\xa8\tLv\x1e\xc0\x9b\xd0C\xb0\n'

Here is the code:

import os 
import numpy as np
def readdata():
  filename1 = "data.bin"
  f1 = open (filename1,'rb')

f1stat = os.stat(filename1)

count = f1stat.st_size

data = np.zeros((5,count), dtype= np.float)

for i in range(5):
  for j in range(count):
      data[i][j] = f1.readline()

CodePudding user response:

You can actually directly read the binary file into a numpy array by using np.fromfile and setting the data type to the length of your integer in bits, for example, uint32

f1 = open(filename1,'rb')
data = np.fromfile(f1, dtype=np.uint32)

You can read more here

  • Related