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
#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


Messages In This Thread
RE: Need help reading in a binary file - by Larz60+ - Jun-21-2017, 11:31 PM
RE: Need help reading in a binary file - by zivoni - Jun-22-2017, 10:04 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Reading large crapy text file in anaconda to profile data syamatunuguntla 0 838 Nov-18-2022, 06:15 PM
Last Post: syamatunuguntla
  How to form a dataframe reading separate dictionaries from .txt file? Doug 1 4,265 Nov-09-2020, 09:24 AM
Last Post: PsyPy
  reading tab file Mandiph 1 2,204 Sep-05-2019, 01:03 PM
Last Post: ThomasL
  Reading a .dat file in Python mohd_umair 4 23,610 Apr-24-2019, 12:07 PM
Last Post: mohd_umair
  Reading time steps from nc file ankurk017 1 2,603 Jul-16-2018, 07:06 PM
Last Post: woooee
  reading, modifying and writing json file metalray 2 10,953 Jun-06-2018, 03:09 PM
Last Post: metalray
  Reading json file as pandas data frame? Alberto 1 8,381 Feb-05-2018, 12:43 AM
Last Post: snippsat
  matplotlib help with reading values from CSV file cps13 6 7,342 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