struct模块拆分字符串为指定字符数的字符串或元组

import struct

# unpack() parses the string to a tuple
format = '1s2s1s1s'
line = '12345'
col = struct.unpack(format, line)
print col

# calcsize() returns the number of characters
# in a given format string
format = '30s30s20s1s'
print struct.calcsize(format)

struct.unpack(format,string)按照format格式将string解压为元组。

struct.pack(format,v1,v2,...) 按照format格式将v1,v2压缩成单一字符串

struct.calcsize(format) 返回format的字符总数。

 

posted on 2017-12-27 20:52  BioinformaticsMaster  阅读(219)  评论(0编辑  收藏  举报

导航