- 论坛徽章:
- 0
|
1. 设置fomat格式,如下:# 取前5个字符,跳过4个字符华,再取3个字符
format = '5s 4x 3s' 复制代码2. 使用struck.unpack获取子字符串import struct
print struct.unpack(format, 'Test astring')
#('Test', 'ing') 复制代码来个简单的例子吧,有一个字符串'He is not very happy',处理一下,把中间的not去掉,然后再输出。import struct
theString = 'He is not very happy'
format = '2s 1x 2s 5x 4s 1x 5s'
print ' '.join(struct.unpack(format, theString)) 复制代码输出结果:
He is very happy
本文来自: 脚本之家(
www.jb51.net
) 详细出处参考:
http://www.jb51.net/article/15705.htm
本文来自ChinaUnix博客,如果查看原文请点:http://blog.chinaunix.net/u3/97516/showart_1950064.html |
|