Python Forum
Need help reading in a binary file
Thread Rating:
  • 2 Vote(s) - 1.5 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Need help reading in a binary file
#1
I need help figuring out how to read in a binary file, and then placing it into a numpy array.

The error I am getting is: AttributeError: 'ReadBinary' object has no attribute 'array'

Thank you in advance.

import numpy as np
import collections

class ReadBinary(collections.Mapping):
    '''
    Reads in Binary Code.
    '''
    filename = 'R033P010BDAS.eud'
    
    def __init__(self):
        self.cache= []
        with open(self.filename, 'rb') as self.file:
            self.filecontent = self.file.read()
           
    def __len__(self):
        return len(self.filecontent)
    
    def __iter__(self):
        self.cache_index = 0
        return self
    
    def __next__(self):
        self.cache_index += 1
        if len(self.filecontent) >= self.cache_index:
            return self.cache[self.cache_index-1]
        
        if self.file.closed:
            raise StopIteration
        row= self.filecontent.read()
        
        if not row:
            self.file.close()
            raise StopIteration
            
        array = np.fromstring(self.filecontent)
        self.cache.append(array)
        
        return array
    
    def __getitem__():
        pass

read = ReadBinary()

print(read.array)
Reply
#2
Please show the entire traceback. It contains valuable information
and I believe part of the prblem is that you need to append
.decode('utf-8') to your read statement, like:
self.filecontent = self.file.read().decode('utf-8')
Reply
#3
There is no array attribute defined in your class, so its correct that read.array raises AttributeError.

As you have implemented __next__ and __iter__, it seems that proper way to use your class would be
read = iter(ReadBinary())  # to get iterator
next(read)  # should return first array value ...
Without calling iter first next(read) would raise error (no self.cache_index defined in __init__).

Unfortunately there are other problems too, for example self.filecontent is a binary string (without .read() method), so self.filecontent.read() would raise error.  Morever self.filecontent contains entire file, therefore entire logic in __next__ seems rather questionable (and erroneous).

I am not sure what do you want to do. If you need to read entire file as single 1D numpy array, then you can do it directly (see numpy.fromfile). And if you want to read rows (there are no implicit rows in binary file...) as separate numpy arrays, then perhaps using generator expression that reads one "row" and yields appropriate numpy array would be better way (you could add caching on top of that generator expression).
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Reading large crapy text file in anaconda to profile data syamatunuguntla 0 811 Nov-18-2022, 06:15 PM
Last Post: syamatunuguntla
  How to form a dataframe reading separate dictionaries from .txt file? Doug 1 4,197 Nov-09-2020, 09:24 AM
Last Post: PsyPy
  reading tab file Mandiph 1 2,149 Sep-05-2019, 01:03 PM
Last Post: ThomasL
  Reading a .dat file in Python mohd_umair 4 23,473 Apr-24-2019, 12:07 PM
Last Post: mohd_umair
  Reading time steps from nc file ankurk017 1 2,542 Jul-16-2018, 07:06 PM
Last Post: woooee
  reading, modifying and writing json file metalray 2 10,883 Jun-06-2018, 03:09 PM
Last Post: metalray
  Reading json file as pandas data frame? Alberto 1 8,307 Feb-05-2018, 12:43 AM
Last Post: snippsat
  matplotlib help with reading values from CSV file cps13 6 7,230 Aug-22-2017, 10:21 AM
Last Post: andre

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020