Python Cookbook学习记录 ch1_13_2013/10/24

1.13访问子字符串

a.使用切片,但是切片一次只能取得一个字段

b.使用struct.unpack方法

import struct
# Get a 5-byte string, skip 3, get two 8-byte strings, then all the rest:
baseformat = "5s 3x 8s 8s"
# by how many bytes does theline exceed the length implied by this
# base-format (24 bytes in this case, but struct.calcsize is general)
numremain = len(theline) - struct.calcsize(baseformat)
# complete the format with the appropriate 's' field, then unpack
format = "%s %ds" % (baseformat, numremain)
l, s1, s2, t = struct.unpack(format, theline)

c.如果需要获取5字节一组的数据,可以将切片应用到列表推导方法。

>>> theline='ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz'
>>> fives=[theline[k:k+5] for k in xrange(0,len(theline),5)]
>>> print fives
['ABCDE', 'FGHIJ', 'KLMNO', 'PQRST', 'UVWXY', 'Zabcd', 'efghi', 'jklmn', 'opqrs', 'tuvwx', 'yz']

d.如果只需要单独的字符更加简单,直接使用list方法

>>> print list('hello')
['h', 'e', 'l', 'l', 'o']

e.将数据切成指定长度的列:
还是将切片应用到列表推导方法,其中很巧妙的应用了zip方法,此方法返回一个一个数对,分别对应(cuts[k],cuts[k+1])

并且包括了(0,cuts[0])和(cuts[len(cuts)-1],None)

>>> theline='ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz'
>>> cuts = [8,14,20,26,30]
>>> pieces = [theline[i:j] for i,j in zip([0]+cuts,cuts+[None])]
>>> print pieces
['ABCDEFGH', 'IJKLMN', 'OPQRST', 'UVWXYZ', 'abcd', 'efghijklmnopqrstuvwxyz']

zip方法是将两个列表糅在一起,因为要一一对应,所以以较短列表的长度为准:

>>> x = [1,2,3]
>>> y = [4,5,6,7]
>>> print zip(x,y)
[(1, 4), (2, 5), (3, 6)]

 

posted on 2013-10-24 23:17  七海之风  阅读(175)  评论(0)    收藏  举报

导航