访问子字符串最简单的的方式是使用切片
afiled=theline[3:8]但一次只能取一个子字符串
如果还要考虑字段的长度struct.unpack可能更合适
importstruct
#得到一个5字节的字符串跳过三字节得到两个8字节的字符串以及其余部分
baseformat="5s3x8s8s"
#theline超出的长度也由这个base-format确定
numremain=len(theline)-struct.calcsize(baseformat)
#用合适的s或者x字段完成格式然后unpack
format="%s%ds"%(baseformat,numremain)
l,s1,s2,t=struct.unpack(format,theline)#test
>>>theline="numremain=len(theline)-struct.calcsize(baseformat)"
>>>numremain=len(theline)-struct.calcsize(baseformat)
>>>format="%s%ds"%(baseformat,numremain)
>>>format
'5s3x8s8s30s'
>>>l,s1,s2,t=struct.unpack(format,theline)
>>>l
'numre'
>>>s1
'n=len('
>>>s2