Chinaunix首页 | 论坛 | 博客
  • 博客访问: 19091
  • 博文数量: 9
  • 博客积分: 1436
  • 博客等级: 上尉
  • 技术积分: 100
  • 用 户 组: 普通用户
  • 注册时间: 2009-05-27 10:07
文章分类

全部博文(9)

文章存档

2011年(1)

2009年(8)

我的朋友
最近访客

分类: Python/Ruby

2009-05-31 11:32:52

1. 设置fomat格式,如下:
  1. # 取前5个字符,跳过4个字符华,再取3个字符
  2. format = '5s 4x 3s'
复制代码
2. 使用struck.unpack获取子字符串
  1. import struct
  2. print struct.unpack(format, 'Test astring')
  3. #('Test', 'ing')
复制代码
来个简单的例子吧,有一个字符串'He is not very happy',处理一下,把中间的not去掉,然后再输出。
  1. import struct
  2. theString = 'He is not very happy'
  3. format = '2s 1x 2s 5x 4s 1x 5s'
  4. print ' '.join(struct.unpack(format, theString))
复制代码
输出结果:
He is very happy

本文来自: 脚本之家() 详细出处参考:
阅读(621) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~