python struct


calcsize(fmt)   fmt爲格式化字符串,返回格式化字符串所佔字節大小
      好比fmt=">5sH" calcsize(fmt) 輸出7 5s2個字節,H2個字節
(fmt, v1, v2, ...)
    按fmt將v1,v2格式化存儲並返回一個字符串
(fmt, buffer, offset, v1, v2, ...) pack完的數據存放到buffer中偏移offset的位置,offset必須指定 (fmt, string) Unpack the string containing packed C structure data, according to fmt. Requires len(string)
== calcsize(fmt). unpack_from(fmt,content,offset)
      按fmt中格式從context偏移
offset處解析出tuple對象packpack_intounpack
      Unpack the buffer, containing packed C structure data, according to fmt,
       starting at offset. Requires len(buffer[offset:])
>= calcsize(fmt).
  

搬磚整理python

 

fmt:來自python幫助文檔ui

 


The optional first format char indicates byte order, size
and alignment: spa

  @: native order, size & alignment (default)
  =: native order, std. size & alignment
  <: little-endian, std. size & alignment
  >: big-endian, std. size & alignment
  !: same as >

 

import struct

#----------------------------------------------------------------------
def main():
    """"""
    fmt =     ">5sH"
    pbuf  = struct.pack(fmt,"11111",1)
    open("1.txt","wb").write(pbuf)

    
    readbuf=open("1.txt","rb").read()
    tp=struct.unpack(fmt,readbuf)

    for i in tp:
        print i


if __name__ == '__main__':

    main()

輸出:code

相關文章
相關標籤/搜索