pytho you basic programming: a method of data format conversion module in the Python struct

Python is a very simple language, the data type representation, unlike many other types of predefined languages ​​(eg: in C #, on the light integer defines eight), it only defines six basic types: string, integer, floating-point numbers, tuples, lists, dictionaries. By these six types of data, we can do most of the work. But Python need time to interact over a network with other platforms, it must take into account the type between these data types with other platforms or languages ​​mutual conversion issues. Analogy: C ++ to write the client sends an int data type (4 bytes) variables to the server written in Python, Python receives 4-byte integers to represent this data, how to parse Python integer know it? Python's standard module struct will be used to solve this problem.

struct module content much, not too difficult, the most commonly used methods below are described wherein:
struct.pack

struct.pack Python values ​​for character based format, converted to a string (Python because no byte (Byte) type, where the string can be understood as a byte stream, or byte array). Which is a function prototype: struct.pack (fmt, v1, v2, ...), the parameter string format is fmt. v1, v2, ... represent python value to be converted. The following example converts the two integers to strings (byte stream):

import struct
  
a = 20
b = 400
  
str = struct.pack("ii", a, b) 
#转换后的str虽然是字符串类型,但相当于其他语言中的字节流(字节数组),可以在网络上传输
print 'length:', len(str)
print str
print repr(str)
  
#---- result
#length: 8
#  ----这里是乱码
#'/x14/x00/x00/x00/x90/x01/x00/x00'

Specifier "i" represents convert int, 'ii' represents two variables int. Results length after conversion is 8 bytes (4 bytes int type, int two 8 bytes), you can see the output result is garbled, since the result is binary data, it is garbled. Built-in functions may be used to acquire repr python recognizable character string, wherein the hexadecimal 0 × 00000014, 0 × 00001009, respectively 20 and 400.
struct.unpack

struct.unpack do the work coincides with the struct.pack contrast, for data type byte stream into a python. It is the function prototype: struct.unpack (fmt, string), the function returns a tuple. The following is a simple example:

str = struct.pack("ii", 20, 400)
a1, a2 = struct.unpack("ii", str)
print 'a1:', a1
print 'a2:', a2
  
#---- result:
#a1: 20
#a2: 400
struct.calcsize

struct.calcsize length for calculated results corresponding to the format string, such as: struct.calcsize ( 'ii'), returns 8. Because the two types of occupied int length is 8 bytes.
struct.pack_into, struct.unpack_from

These two functions has been introduced in Python manual, but no examples are given of how to use. In fact, they are not much used in practice. Google for a long time, to find an example, posted about sharing:

import struct
from ctypes import create_string_buffer
  
buf = create_string_buffer(12)
print repr(buf.raw)
  
struct.pack_into("iii", buf, 0, 1, 2, -1)
print repr(buf.raw)
  
print struct.unpack_from('iii', buf, 0)
  
#---- result
#'/x00/x00/x00/x00/x00/x00/x00/x00/x00/x00/x00/x00'
#'/x01/x00/x00/x00/x02/x00/x00/x00/xff/xff/xff/xff'
#(1, 2, -1)

About the format string

In Python manual, given the type commonly used in C language with Python format corresponding to the type of character: Here Insert Picture Description
the last word of mouth to recommend a good python gathering [ click to enter ], there are a lot of old-timers learning skills, learning experience, interview skills, experience and other workplace share, the more we carefully prepared the zero-based introductory information, information on actual projects, the timing has to explain the Python programmer technology every day, share some learning methods and the need to pay attention to small details

Published 50 original articles · won praise 34 · views 70000 +

Guess you like

Origin blog.csdn.net/haoxun08/article/details/104909335