当前位置:网站首页>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 (*◡‿◡)

边栏推荐
- Reading sdwebimage source code Notes
- With a monthly salary of 15.5K, he failed to start a business and was heavily in debt. How did he reverse the trend through software testing?
- What is the reason why the easycvr national standard protocol access equipment is online but the channel is not online?
- Simulink automatically generates STM32 code details
- Barbie q! How to analyze the new game app?
- Design and development of C language ATM system project
- Improving the readability of UI layer test with puppeter
- Have you ever seen this kind of dynamic programming -- the stock problem of state machine dynamic programming (Part 2)
- 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
- Using RAC to realize the sending logic of verification code
猜你喜欢
![[paper notes] - low illumination image enhancement - zeroshot - rrdnet Network - 2020-icme](/img/e3/f9c6bfdbcd5dffd406e3f1d2331050.png)
[paper notes] - low illumination image enhancement - zeroshot - rrdnet Network - 2020-icme

Method of converting UI file to py file

With a monthly salary of 15.5K, he failed to start a business and was heavily in debt. How did he reverse the trend through software testing?

Easycvr Video Square snapshot adding device channel offline reason display

Driving the powerful functions of EVM and xcm, how subwallet enables Boca and moonbeam

HashSet add

【ARIXV2204】Neighborhood attention transformer

Paper reading notes -- crop yield prediction using deep neural networks

【ARXIV2204】Vision Transformers for Single Image Dehazing

Check box error
随机推荐
FreeRTOS startup process, coding style and debugging method
【ARXIV2203】SepViT: Separable Vision Transformer
Table image extraction based on traditional intersection method and Tesseract OCR
Configuration experiment of building virtual private network based on MPLS
MySQL(5)
Program life | how to switch to software testing? (software testing learning roadmap attached)
list indices must be integers or slices, not tuple
FPGA:使用PWM波控制LED亮度
App test process and test points
Dcgan:deep volume general adaptive networks -- paper analysis
How to successfully test php7.1 connecting to sqlserver2008r2
Introduction to testcafe
Service object creation and use
MySQL 默认隔离级别是RR,为什么阿里等大厂会改成RC?
Analysis of the reason why easycvr service can't be started and tips for dealing with easy disk space filling
RT_ Use of thread message queue
面试了一位38岁程序员,听说要加班就拒绝了
HDU 3585 maximum shortest distance
Improving the readability of UI layer test with puppeter
100 lectures on Excel practical application cases (XI) - tips for inserting pictures in Excel