Python Glossary Русский

all Python 3structfunction

struct.unpack_from()

packing and unpacking

Unpacks values by a struct format from a buffer starting at a given offset; extra bytes after the record are ignored.

Syntax

values = struct.unpack_from(format, /, buffer, offset=0)

Unlike unpack, the buffer may be longer than the record — that is how you read a file header or a field in the middle of a packet without slicing and copying. offset counts bytes, not records: for record i it is i * calcsize(format). If there are not enough bytes after offset, struct.error reports the actual buffer size.

Examples

import struct
data = b'\x00\x01\x02\x03'
print(struct.unpack_from('<H', data, 1))  # → (513,)
print(struct.unpack_from('<H', data))  # → (256,) — хвост не мешает
import struct
try:
    struct.unpack_from('<I', b'ab')
except struct.error as e:
    print(type(e).__name__)  # → error

See also

Common errors

Open in the full glossarydocs.python.orgReport an inaccuracy