Python binary file parsing

Source: Internet
Author: User

Struct.pack ():

Struct.pack is used to convert Python values from a format character to a string (because there is no byte type in Python, you can interpret the string here as a byte stream, or an array of bytes). Its function prototype is: Struct.pack (FMT, V1, v2, ...) , the parameter FMT is a format string, with information about the format string below some introduction. V1, v2, ... Represents the Python value to convert.

Struct.unpack

Struct.unpack does work just as opposed to struct.pack, which is used to convert byte streams into Python data types. The function prototype is: Struct.unpack (FMT, String), which returns a tuple.

1F= Open (file_name,'RB')2F.seek (0,2)3data_len=F.tell ()4  while1:5      ifF.tell () = =Data_len:6          Print 'file is end'7           Break8      Else:9data = F.read (2)Ten(ident,) = Struct.unpack (endianness +'H', Data[0:2]) OneF.close ()

Python binary file parsing

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.