7

struct.unpack will unpack data into a tuple. Is there an equivalent that will store data into a dict instead?

In my particular problem, I am dealing with a fixed-width binary format. I want to be able, in one fell swoop, to unpack and store the values in a dict (currently I manually walk through the list and assign dict values)

Foo Bah
  • 25,660
  • 5
  • 55
  • 79

3 Answers3

11

If you're on 2.6 or newer you can use namedtuple + struct.pack/unpack like this:

import collections
import struct

Point = collections.namedtuple("Point", "x y z")

data = Point(x=1, y=2, z=3)

packed_data = struct.pack("hhh", *data)
data = Point(*struct.unpack("hhh", packed_data))

print data.x, data.y, data.z
PabloG
  • 25,761
  • 10
  • 46
  • 59
9

Do you want something like this?

keys = ['x', 'y', 'z']
values = struct.unpack('<III', data)
d = dict(zip(keys, values))
FogleBird
  • 74,300
  • 25
  • 125
  • 131
6

The struct documentation shows an example of unpacking directly into a namedtuple. You can combine this with namedtuple._asdict() to get your one swell foop:

>>> import struct
>>> from collections import namedtuple
>>> record = 'raymond   \x32\x12\x08\x01\x08'
>>> Student = namedtuple('Student', 'name serialnum school gradelevel')
>>> Student._asdict(Student._make(struct.unpack('<10sHHb', record)))
{'school': 264, 'gradelevel': 8, 'name': 'raymond   ', 'serialnum': 4658}
>>> 

If it matters, note that in Python 2.7 _asdict() returns an OrderedDict...

mtrw
  • 34,200
  • 7
  • 63
  • 71
  • Heh probably should have checked the web doc. No indication of this in the python `help(struct)` – Foo Bah Aug 23 '11 at 03:03
  • 3
    interesting answer. i see it's a dynamically generated function, but why is _asdict() with a prepended underscore? – wim Aug 23 '11 at 03:58
  • Using private methods - great idea. – FogleBird Aug 24 '11 at 00:44
  • 1
    @FogleBird - not sure what you mean by private methods. `namedtuple._asdict()` is documented here: http://www.python.org/doc//current/library/collections.html#collections.somenamedtuple._asdict. And I took the usage of `namedtuple._make()` directly from the documentation linked in the answer. What's the recommended best practice here? – mtrw Aug 24 '11 at 01:38
  • The spaces after raymond makes me crazy to debug the code. – coanor Feb 06 '14 at 12:44
  • Why the code can not run under python3: `TypeError: 'str' does not support the buffer interface`? – coanor Feb 06 '14 at 12:49
  • 1
    You can rewrite the fifth line more concisely as `Student(*struct.unpack('<10sHHb', record))._asdict()` – Alastair Irvine Nov 20 '18 at 09:22
  • 1
    this solution is significantly slower than `dict(zip(keys,struct.unpack(...)))` – Joran Beasley Dec 21 '19 at 21:46