当前位置:网站首页>How practical is the struct module? Learn a knowledge point immediately
How practical is the struct module? Learn a knowledge point immediately
2022-07-28 05:12:00 【Panda aiqia rice】
Hey, everyone, good duck ! I'm a panda
Today, let me tell you about a very practical python modular ( •̀ ω •́ )*
struct The module provides a string of bytes and Python Conversion functions between native data types , Such as numbers and strings .
The function of this module is to complete Python Sum of values C Of language structure Python Conversion between string forms .
This can be used to process binary data stored in files or from network connections , And other data sources .
what are you having? python I won't answer the related error report 、 Or source code information / Module installation /
Women's clothing bosses are proficient in skills
You can come here :(https://jq.qq.com/?_wv=1027&k=2Q3YTfym) perhaps +V:python10010 Ask me
1. Module functions and Struct class
In addition to providing a Struct Outside class ,
There are also many module level functions for dealing with structured values .
Here is a formatter (Format specifiers) The concept of ,
It refers to the conversion from string format to compiled representation ,
Similar to regular expression processing .
Usually instantiate Struct class ,
Call class methods to complete the transformation ,
It is much more effective than calling module functions directly .
The following examples all use Struct class .
2. Packing( pack ) and Unpacking( Unpack )
Struct Support the transfer of data packing( pack ) String ,
And can reverse from the string unpacking( decompression ) Data .
In this case , Format specifier (specifier) Need an integer or long integer ,
A two byte string, And a floating point number .
Spaces in the formatter are used to separate indicators (indicators),
It will be ignored when compiling the format .
import struct
python Learning exchange group :660193417###
import binascii
values = (1, 'ab'.encode('utf-8'), 2.7)
s = struct.Struct('I 2s f')
packed_data = s.pack(*values)
print(' Original value :', values)
print(' Format symbol :', s.format)
print(' Occupied bytes :', s.size)
print(' Packing results :', binascii.hexlify(packed_data))
# output
Original value : (1, b'ab', 2.7)
Format symbol : b'I 2s f'
Occupied bytes : 12
Packing results : b'0100000061620000cdcc2c40'
This example converts the packed value into a hexadecimal byte sequence ,
use binascii.hexlify() Method print out .
Use unpack() How to unpack .
import struct
import binascii
packed_data = binascii.unhexlify(b'0100000061620000cdcc2c40')
s = struct.Struct('I 2s f')
unpacked_data = s.unpack(packed_data)
print(' Unpacking results :', unpacked_data)
# output
Unpacking results : (1, b'ab', 2.700000047683716)
Pass the packed value to unpack(), Basically return the same value ( Floating point numbers will vary ).
3. Byte order / size / alignment
By default ,pack It's using local C The byte order of the Library .
The first character of the format string can be used to indicate the byte order of the fill data 、 Size and alignment , The following table describes :

If these are not set in the formatter , Then the default will be @.
Local byte order means that the byte order is determined by the current host system .
such as :Intel x86 and AMD64(x86-64) Use small byte order ;
Motorola 68000 and PowerPC G5 Use large byte order .
ARM and Intel Itanium supports switching byte order .
have access to sys.byteorder Check the byte order of the current system .
Local size (Size) And alignment (Alignment) By c Compiler sizeof The expression determines . It corresponds to the local byte order .
The standard size is determined by the formatter , Next, we will talk about the standard sizes of various formats .
Example :
import struct
import binascii
values = (1, 'ab'.encode('utf-8'), 2.7)
print(' Original value : ', values)
endianness = [
('@', 'native, native'),
('=', 'native, standard'),
('<', 'little-endian'),
('>', 'big-endian'),
('!', 'network'),
]
for code, name in endianness:
s = struct.Struct(code + ' I 2s f')
packed_data = s.pack(*values)
print()
print(' Format symbol : ', s.format, 'for', name)
print(' Occupied bytes : ', s.size)
print(' Packing results : ', binascii.hexlify(packed_data))
print(' Unpacking results : ', s.unpack(packed_data))
# output
Original value : (1, b'ab', 2.7)
Format symbol : b'@ I 2s f' for native, native
Occupied bytes : 12
Packing results : b'0100000061620000cdcc2c40'
Unpacking results : (1, b'ab', 2.700000047683716)
Format symbol : b'= I 2s f' for native, standard
Occupied bytes : 10
Packing results : b'010000006162cdcc2c40'
Unpacking results : (1, b'ab', 2.700000047683716)
Format symbol : b'< I 2s f' for little-endian
Occupied bytes : 10
Packing results : b'010000006162cdcc2c40'
Unpacking results : (1, b'ab', 2.700000047683716)
Format symbol : b'> I 2s f' for big-endian
Occupied bytes : 10
Packing results : b'000000016162402ccccd'
Unpacking results : (1, b'ab', 2.700000047683716)
Format symbol : b'! I 2s f' for network
Occupied bytes : 10
Packing results : b'000000016162402ccccd'
Unpacking results : (1, b'ab', 2.700000047683716)
4. Format symbol
The comparison table of format characters is as follows :

5. buffer
Packaging data into binary is usually used in scenarios that require high performance .
In such scenarios, it can be optimized by avoiding the overhead of allocating new buffers for each packaging structure .
pack_into() and unpack_from() Method supports writing directly to the pre allocated buffer .
That's all for today's knowledge , I hope this article can help you ~
I'm a panda , See you in the next article (*◡‿◡)

边栏推荐
- 【CVPR2022 oral】Balanced Multimodal Learning via On-the-fly Gradient Modulation
- POJ 3728 the merchant (online query + double LCA)
- What is the core value of testing?
- 【ARXIV2203】CMX: Cross-Modal Fusion for RGB-X Semantic Segmentation with Transformers
- RT_ Use of thread message queue
- Easycvr Video Square snapshot adding device channel offline reason display
- Redis配置文件详解/参数详解及淘汰策略
- HDU 2874 connections between cities
- Test report don't step on the pit
- Simulink automatically generates STM32 code details
猜你喜欢

如何在 FastReport VCL 中通过 Outlook 发送和接收报告?

The solution after the samesite by default cookies of Chrome browser 91 version are removed, and the solution that cross domain post requests in chrome cannot carry cookies

Read the paper -- a CNN RNN framework for clip yield prediction

The most detailed installation of windows10 virtual machine, install virtual machine by hand, and solve the problem that the Hyper-V option cannot be found in the home version window

数据安全逐步落地,必须紧盯泄露源头

Inspire domestic students to learn robot programming education for children

Design and development of C language ATM system project

Table image extraction based on traditional intersection method and Tesseract OCR

CPU and memory usage are too high. How to modify RTSP round robin detection parameters to reduce server consumption?

【ARXIV2204】Simple Baselines for Image Restoration
随机推荐
Message forwarding mechanism -- save your program from crashing
Angr (XI) - official document (Part2)
Melt cloud x chat, create a "stress free social" habitat with sound
Win10 machine learning environment construction pycharm, anaconda, pytorch
Interpreting the source code of cfrunloopref
Gym 101911c bacteria (minimum stack)
Analysis of the reason why easycvr service can't be started and tips for dealing with easy disk space filling
从微服务基本概念到核心组件-通过一个实例来讲解和分析
猿辅导技术进化论:助力教与学 构想未来学校
POJ 2763 housewife wind (tree chain partition + edge weighting point weight)
Barbie q! How to analyze the new game app?
Keil Chinese garbled code solution
The solution after the samesite by default cookies of Chrome browser 91 version are removed, and the solution that cross domain post requests in chrome cannot carry cookies
Redis configuration file explanation / parameter explanation and elimination strategy
阿里怎么用DDD来拆分微服务?
【CVPR2022 oral】Balanced Multimodal Learning via On-the-fly Gradient Modulation
Data imbalance: comprehensive sampling of anti fraud model (data imbalance)
MySQL(5)
System clock failure of database fault tolerance
类和对象【中】