Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
struct.unpack failed
#3
On the line that's erroring (line 9), you are using struct.unpack. You are passing in "bytearray". In python bytearray is a type that an object can be.
It seems you want to unpack the header of the file. In that case you would want to read the file into a byte array and pass that in instead.
This should work:
b_array = bytearray(f.read())
_ignored, n_images, image_columns, image_rows = struct.unpack('>IIII', b_array)
EDIT: I did a quick test. For me, running that code gave me an error saying it wanted a 16 byte buffer. I had to do this instead:
_ignored, n_images, image_columns, image_rows = struct.unpack('>IIII', bytearray(a.read()[:16]))

You may need that, you may not.
Reply


Messages In This Thread
struct.unpack failed - by Roro - Jun-13-2020, 04:24 PM
RE: struct.unpack failed - by deanhystad - Jun-13-2020, 05:20 PM
RE: struct.unpack failed - by DreamingInsanity - Jun-13-2020, 05:28 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Too much values to unpack actualpy 3 538 Feb-11-2024, 05:38 PM
Last Post: deanhystad
  Python Struct Question, decimal 10, \n and \x0a. 3python 2 697 Aug-11-2023, 09:29 AM
Last Post: 3python
  unpack dict menator01 1 1,247 Apr-09-2022, 03:10 PM
Last Post: menator01
  ValueError: not enough values to unpack (expected 4, got 1) vlearner 2 6,402 Jan-28-2022, 06:36 PM
Last Post: deanhystad
  JS Buffer.from VS struct.pack DreamingInsanity 3 2,533 Apr-05-2021, 06:27 PM
Last Post: DreamingInsanity
  [SOLVED] [geopy] "ValueError: too many values to unpack (expected 2)" Winfried 2 2,921 Mar-30-2021, 07:01 PM
Last Post: Winfried
  Cannot unpack non-iterable NoneType object, i would like to ask for help on this. Jadiac 3 9,010 Oct-18-2020, 02:11 PM
Last Post: Jadiac
  subprogram issues: cannot unpack non-iterable function object error djwilson0495 13 6,146 Aug-20-2020, 05:53 PM
Last Post: deanhystad
  Pack integer values as single bytes in a struct bhdschmidt 3 2,408 Jun-09-2020, 09:23 PM
Last Post: bhdschmidt
  struct.decode() and '\0' deanhystad 1 3,285 Apr-09-2020, 04:13 PM
Last Post: TomToad

Forum Jump:

User Panel Messages

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