maple412 发表于 2015-04-30 14:51

关于struct.unpack的用法咨询

baseformat="5s 3x 8s 8s"
theline="abcdedfgeghgabcdedfgeghg"
remain=len(theline)-struct.calcsize(baseformat)
fmt="%s,%ds" % (baseformat,remain)
struct.unpack(fmt,theline)
执行报错,提示:error: bad char in struct format 这个是怎么回事

我理解unpack的作用就是将字符串拆分成baseformat的格式,比如5s 3x 8s 8s代表首先5个字节,然后跳过3个,再接着两个8个字节的数据,这样理解对吗

另外看unpack的解释:最后一句话什么意思,string的长度必须和calcsize(fmt)的长度一样?
unpack(fmt, string)
Unpack the string (presumably packed by pack(fmt, ...)) according to the given format. The result
is a tuple even if it contains exactly one item. The string must contain exactly the amount of data required
by the format (len(string) must equal calcsize(fmt)).
页: [1]
查看完整版本: 关于struct.unpack的用法咨询