代码如下:
#encoding=utf-8 print '中国' #访问子字符串 #方案一 切片 一次只能取一个 print '------1' str ='abc中国' ustr =u'abc中国' print str[2:6] #相当于2-5 中文占了2个 因为是UTF-8 print ustr[2:4] #unicode #方案二 struct.unpack 考虑长度 print '-----2' import struct theline='abcd12345678901234567890jklm' baseformat = '5s 3x 8s 8s' remain = len(theline) - struct.calcsize(baseformat) print remain format = '%s %ds'%(baseformat,remain) print format l,s1,s2,t= struct.unpack(format, theline) print l print s1 print s2 print t l,s1,s2 = struct.unpack(baseformat,theline[:struct.calcsize(baseformat)]) print l print s1 print s2 #方案三 列表推导方法 n个字节一组 print '------3' n = 5 print [theline[k:k+5] for k in xrange(0,len(theline),5)] #xrang 0开始len(theline)结束 步长为5 #切割为一个数组 chars =list(theline) print chars #方案四 切割成指定长度 print '------4' cuts = [8,14,20,26,27] pieces = [theline[i:j] for i,j in zip([0]+cuts,cuts+[None])] print pieces #封装 一 unpack def fields(baseformat,theline,lastfiled=False): remain = len(theline) - struct.calcsize(baseformat) format = '%s %ds'%(baseformat,remain,lastfiled and 's' or 'x') return struct.unpack(format,theline) #封装三 slices 等长 def fields1(theline,n,lastfiled=False): pieces = [theline[k:k+n] for k in xrange(0,len(theline),5)] if not lastfiled or len(pieces[-1]) < n: pieces.pop() return pieces #封装 一 slices 变长 def fields2(theline,cuts,lastfiled=False): pieces = [theline[i:j] for i,j in zip([0]+cuts,cuts+[None])] if not lastfiled: pieces.pop() return pieces #study zip print [0]+[1,2] print [5]+[6,7] print zip([0]+[1,2],[5]+[6,7])
运行结果如下:
中国
------1
c中
c中
-----2
4
5s 3x 8s 8s 4s
abcd1
56789012
34567890
jklm
abcd1
56789012
34567890
------3
['abcd1', '23456', '78901', '23456', '7890j', 'klm']
['a', 'b', 'c', 'd', '1', '2', '3', '4', '5', '6', '7', '8', '9', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '0', 'j', 'k', 'l', 'm']
------4
['abcd1234', '567890', '123456', '7890jk', 'l', 'm']
[0, 1, 2]
[5, 6, 7]
[(0, 5), (1, 6), (2, 7)]