On this page
struct.unpack
Prev: struct.Struct | Up: struct | Next: struct.unpack_from
Rel: struct.calcsize, struct.error, struct.iter_unpack, struct.pack
Ref: docs.python.org — struct.unpack
Tags: #public #python3 #stdlib #struct
Import
import struct
from struct import unpack
Signature & Overview
struct.unpack(format, buffer, /)
Return a tuple containing values unpacked according to the format string.
Usage
import struct
version, length = struct.unpack("!BH", b"\x01\x00\x2a")
assert (version, length) == (1, 42)
Gotchas
- Always returns a tuple—even for a single field: Write
(val,) = struct.unpack("!I", raw)(with a trailing comma).
REPL Inspect
from struct import unpack
help(unpack)
Linked from
Related notes
- tuts/python3/stdlib_module/struct/Struct Linked here; links to this page; shared tags: #stdlib, #struct
- tuts/python3/stdlib_module/struct/unpack_from Linked here; links to this page; shared tags: #stdlib, #struct
- tuts/python3/stdlib_module/struct/calcsize Linked here; shared tags: #stdlib, #struct
- tuts/python3/stdlib_module/struct/error Linked here; shared tags: #stdlib, #struct
- tuts/python3/stdlib_module/struct/iter_unpack Linked here; shared tags: #stdlib, #struct
- tuts/python3/stdlib_module/struct/pack Linked here; shared tags: #stdlib, #struct
- tuts/python3/stdlib_module/struct links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/struct/pack_into shared tags: #stdlib, #struct
- tuts/c/stdlib shared tags: #stdlib
- tuts/c/stdlib/assert shared tags: #stdlib