- Trending Categories
- Data Structure
- Operating System
- MS Excel
- C Programming
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Python Binary Data Services
Provisions of the struct module in the Python library are useful in performing conversions between C type structs and Python bytes objects. This can be achieved by module level functions as well as Struct class and its methods as defined in the struct module.
The conversion functions use a format string. The byte order, size, and alignment used in the format string is determined by formatting character as per the following table
|!||network (= big-endian)||standard||none|
Following table shows format characters used to denote C type variables and corresponding Python types.
|Format||C Type||Python type|
|x||pad byte||no value|
|c||char||bytes of length 1|
Following functions are defined in the struct module
This function returns a bytes object containing the values packed according to the format string format. The formatting characters must match the values required by the format.
This function unpacks from the buffer according to the format string format. The result is a tuple even if it contains exactly one item.
Following code demonstrates the use of these functions.
import struct student=(1, b'Rahul', 65.75) packed=struct.pack('I 5s f', *student) print ('packed data:',packed) unpacked=struct.unpack('I 5s f', packed) print ('unpacked data:',unpacked)
packed data: b'\x01\x00\x00\x00Rahul\x00\x00\x00\x00\x80\x83B' unpacked data: (1, b'Rahul', 65.75)
Packing/unpacking can also be achieved by using methods in the Struct class. Creating a Struct object once and calling its methods is more efficient than calling the struct functions with the same format since the format string only needs to be compiled once.
This constructor returns a new Struct object which writes and reads binary data according to the format string format.
This method is identical to the pack() function, using the compiled format.
This method is identical to the unpack() function, using the compiled format.
Following example shows how Struct class is used to pack and unpack Python data.
s = struct.Struct('I 5s f') packed=s.pack(*student) print (packed) unpacked = s.unpack(packed) print (unpacked)
The packed data can be directly parsed to a named tuple object.
from collections import namedtuple newstudent = namedtuple('newstudent','No Name Marks') s1 = newstudent._make(struct.unpack('I 5s f', packed)) print (s1)
newstudent(No=1, Name=b'Rahul', Marks=65.75)
Kickstart Your Career
Get certified by completing the courseGet Started