You should be familiar with the English word struct, because there is struct in cc ++, where struct is called a struct. Struct is also used in Python, which fully demonstrates that the English word struct is seen in this s, and you should be familiar with it, because there is struct in c/c ++, where struct is called a struct. Struct is also used in Python, which fully demonstrates that this struct should have a deep relationship with struct in c/c ++. Python uses the struct module to convert the Python value and the C struct to form a Python byte object. It uses a format string as a compact description of the underlying C struct and converts it to a Python value based on the format string.
We know that python only defines six data types: string, integer, floating point, list, tuples, and dictionary. But how can I implement some byte variables in C language in python? This is important, especially for data transmission over the network.
Python provides a struct module to provide conversion. The following describes several methods in this module.
Pack (fmt, v1, v2,...) ------ convert the value v1, v2,... into a string based on the format described in the fmt.
Unpack (fmt, bytes) ------ parses bytes reverse based on the format described by the given fmt and returns a tuple.
Calcsize (fmt) ------ return the size of the structure based on the format described by the given fmt.
Struct. pack ():
Struct. pack is used to convert Python values to strings based on format characters (because Python does not have the Byte type, you can understand the strings here as Byte streams or Byte arrays ). The function prototype is struct. pack (fmt, v1, v2,...). The fmt parameter is a format string. The following describes the information about the format string. V1, v2,... indicates the python value to be converted. The following example converts two integers to a string (byte stream ):
>>> Import struct
>>> A = 20
>>> B = 400
>>> Str = struct. pack ('II', a, B) # Convert to throttling. although it is still a string, it can be transmitted over the network.
>>> Print len (str) # ii indicates two int
8 # We can see that the length is 8 bytes, which is exactly the length of two int-type data.
>>> Print str
# Binary is garbled
>>> Print repr (str)
'\ X14 \ x00 \ x00 \ x00 \ x90 \ x01 \ x00 \ x00' # The hexadecimal 0x00000014 and 0x00001009 indicate 20 and 400 respectively.
>>>
Then we can perform any group package. for example, the following package example only describes the pack
Format = "! HH % ds "% len (data)
Buffer = struct. pack (format, opcode, blocknumber, data)
We need to package a data and add some headers. based on the following format characters, we know that H is an unsigned short of 2 bytes, and s is a char type. So this buffer is two bytes of opcode, two bytes of blocknumber, and len long char.
Python module -- struct (byte stream, package splitting implementation)
Struct. unpack ():
We then run the following example:
>>> A1, a2 = struct. unpack ('II', str)
>>> Print 'A1', a1
A1 20
>>> Print 'A2 = ', a2
A2 = 400.
We can see that "ii" divides the eight-byte str into two int integers based on four bytes.
Struct. calcsize (): used to calculate the output size of a specific format, which is several bytes, such:
>>> Struct. calcsize ('hh4s ')
8
>>> Struct. calcsize ('II ')
8
>>>
>>> Format = '! HH % ds '% len ('Hello Python ')
>>> Struct. calcsize (format)
16
>>>