黑客防线,在攻与防的对立统一中寻求突破!2001年创刊的黑客技术专业刊物!

设为首页
收藏本站
联系我们
网站导航

黑客文章 - 黑客编程 - 浏览 - Python天天美味(13) - struct.unpack

Python天天美味(13) - struct.unpack

黑客 发布日期:2008-8-23 0:17:43 共有 228 人次浏览
Python天天美味(13) - struct.unpack
Python中按一定的格式取出某字符串中的子字符串,使用struck.unpack是非常高效的。
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
所属分类: 黑客编程     网摘收藏: Google 雅虎 百度 POCO 365key 和讯 天极