程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Python built-in library struct

編輯:Python

Catalog

struct Brief description of the Library

struct Method

struct.pack()

 struct.unpack()

struct.calcsize(format:str) 

format Usage of parameters

Byte order / size / alignment


struct Brief description of the Library

  struct The module provides a string of bytes and Python Conversion functions between native data types , Such as numbers and strings , Use Python Medium f.write() Function when writing a file , Parameters can only be strings , When operating pure digital writing , inconvenient , This is the time struct Debut .

         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 .

To be exact ,Python There is no data type specifically dealing with bytes . But because of b'str' Can represent bytes , therefore , Byte array = Binary system str. And in the C In language , We can easily use struct、union To handle bytes , And bytes and int,float Transformation .

Therefore, a library is provided for conversion .

struct Method

pack(format, v1, v2, ...) -> bytes String or number , according to format The specified format is converted to bytes The data returned
unpack(fmt, string): Returns a tuple , It contains the value unpacked according to the format string .
calcsize(fmt): Returns the size of the structure described by the format string ( In bytes ).

struct.pack()

         Define a 16 Base string , Convert the string to int After value , Use struct.pack() function , According to the signed int16 type , Write data to file , Loop write 300 Times the same value .

import struct
a = '4FFF'
b = int(a, 16)
with open('test.bin', 'wb') as f:
for x in range(300):
f.write(struct.pack('h', b))

Use 16 Hexadecimal editor to view the file , verification

 struct.unpack()

        struct.unpack(f'{data_num}h', f.read()), Note here , How much data is in the file ,fmt The format of requires multiple , The result returned is a tuple .

import struct
data_num = 300
with open('test.bin', 'rb') as f:
print(struct.unpack(f'{data_num}h', f.read()))

struct.calcsize(format:str) 

         Press format Calculate the number and size of bytes occupied by this format book .

import struct
# B yes 1 Bytes ,H yes 2 Bytes ,I yes 4 Bytes , common 7 Bytes
print(struct.calcsize('<BHI'))
print(struct.calcsize('h'))
print(struct.calcsize('H'))
print(struct.calcsize('i'))
print(struct.calcsize('c'))
print(struct.calcsize('b'))

format Usage of parameters

FormatC TypePython Number of bytes xpad byteNone1ccharint1bsigned charint1Bunsigned charint1?Boolbool1hshortint2Hunsigned shortint2iintint4Iunsigned intint4llongint4Lunsigned longint4qlong longint8Qunsigned long longint8ffloatfloat4ddoublefloat8schar[]bytes1pchar[]bytes1Pvoid *int0

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 :

CharacterByte orderSizealignment@nativenative Make do with it 4 Bytes =nativestandard No change <little-endianstandard No change >big-endianstandard No change !network (= big-endian)standard No change

Reference resources :

Python Use struct Summary of library usage _python_ Script Center - Programming Inn

Python Standard library notes (6) — struct modular - You know


  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved