Python's approach to binary data processing

Source: Internet
Author: User
Tags unpack
The examples in this article describe how Python handles binary data. Share to everyone for your reference. Specific as follows:

#!/usr/env/env python #-*-coding:cp936-*-"" Add Head Infomation for PCM file ' import sys import struct import os __author__ = ' Bob_hu, hewitt924@gmail.com ' __date__ = ' Dec 19,2011 ' __update__ = ' Dec 19,2011 ' def geneheadinfo (Samplerat E,bits,samplenum): "' Generates header information, requires sample rate, number of bits per sample, and the number of bytes sampled for the entire wav ' rheadinfo = ' \x52\x49\x46\x46 ' filelength = Struc T.pack (' i ', Samplenum + $) rheadinfo + = filelength Rheadinfo + = ' \x57\x41\x56\x45\x66\x6d\x74\x20\x10\x00\x00\x00\x01\ x00\x01\x00 ' rheadinfo + = struct.pack (' i ', samplerate) Rheadinfo + = Struct.pack (' i ', samplerate * bits/8) RHeadInfo + = ' \x02\x00 ' rheadinfo + = Struct.pack (' H ', bits) rheadinfo + = ' \x64\x61\x74\x61 ' rheadinfo + = struct.pack (' I ', sample Num) return Rheadinfo if __name__ = = ' __main__ ': If Len (sys.argv)! = 5:print "Usage:python%s inFile samplerate Bits outFile "% sys.argv[0] sys.exit (1) fout = open (sys.argv[4], ' WB ') #用二进制的写入模式 #fout. Write (Struct.pack (' 4s ', ' \x6 6\x6d\x74\x20 ')) #写入一个长A string of 4 degrees, the binary content of this string is 6D, AFD,FAD,AFDD, = Struct.unpack (' 4c ', Fin.read (4)) #读入四个字节, each of which resolves to a letter #open (SYS . argv[4], ' WB '). Write (Struct.pack (' 4s ', ' fmt ') #将字符串解析成二进制后再写入 #open (sys.argv[4], ' WB '). Write (' \x3c\x9c\x00\x00\   X57 ') #直接写入二进制内容: 3C 9C xx #fout. Write (Struct.pack (' I ', 6000) #写入6000的二进制形式 #check whether InFile has Head-info Fin = open (sys.argv[1], ' RB ') Riff_flag, = struct.unpack (' 4s ', Fin.read (4)) if Riff_flag = = ' Riff ': print '%s has header information "% sys.argv[1] Fin.close () sys.exit (0) else:print"%s No header information "% sys.argv[1] fin.close () #采样率 SA mplerate = Int (sys.argv[2]) #bit位 bits = Int (sys.argv[3]) fin = open (sys.argv[1], ' RB ') startpos = Fin.tell () Fin.seek (0,os. Seek_end) Endpos = Fin.tell () Samplenum = (endpos-startpos) print Samplenum headinfo = Geneheadinfo (samp Lerate,bits,samplenum) Fout.write (headinfo) fin.seek (OS. Seek_set) Fout.write (Fin.read ()) Fin.close () Fout.close () 

Hopefully this article will help you with Python programming.

  • Related Article

    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.