Python struct Module parsing _python

Source: Internet
Author: User

Python provides a struct module to provide transformations. Here are some of the methods in this module.

Struct.pack ():

Struct.pack is used to convert a Python value to a string based on the format character (because there is no byte type in Python, the string here can be interpreted as a byte stream, or an array of bytes). Its function prototype is: Struct.pack (FMT, V1, v2, ...), parameter FMT is a format string, and relevant information about the format string is described below. V1, v2, ... Represents the Python value to convert. The following example converts two integers to a string (a byte stream):

Copy Code code as follows:

>>> import struct
>>> a=20
>>> b=400
& Gt;>> str=struct.pack (' II ', a,b)     #转换成字节流, although it is a string, it can be transmitted over the network
>>> Print len (str)                #ii Represents a two int
8                                 # You can see a length of 8 bytes, exactly the length of the two int data
>>> print str
                                 #二进制是乱码
>>> print repr (str)
' \x14\x00\x00\x00\x90\x01\x00\ X00 '    #其中十六进制的 0x00000014, 0x00001009 representing 20 and respectively
>>>

This allows us to arbitrarily group the package, such as the following a packaged example, only to describe the pack

Copy Code code as follows:

Format = "! Hh%ds "% len (data)
Buffer = Struct.pack (format,opcode,blocknumber,data)

We're going to package a data, plus some headers, and we know that H is unsigned short is 2 bytes, and S is char, based on the following format character information. So this buffer is the 2-byte opcode,2 byte Blocknumber, and Len's char.

Struct.unpack ():

We then run the example above:

Copy Code code as follows:

>>> a1,a2=struct.unpack (' II ', str)
>>> print ' A1 ', A1
A1 20
>>> print ' a2= ', A2
A2= 400

You can see that "II" is delimited by four bytes, dividing the 8-byte str into two int integers.

Struct.calcsize (): The size of the output used to compute a particular format, is a few bytes, such as:

Copy Code code as follows:

>>> struct.calcsize (' hh4s ')
8
>>> struct.calcsize (' II ')
8
>>>

>>> format= '! Hh%ds '% len (' Hello python ')
>>> struct.calcsize (format)
16
>>>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.