转载自:http://www.cnblogs.com/coderzh/archive/2008/05/04/1181462.html

Python中按一定的格式取出某字符串中的子字符串,使用struck.unpack是非常高效的。
1. 设置fomat格式,如下:



Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/

--># 取前5个字符,跳过4个字符,再取3个字符
format = '5s 4x 3s'

2. 使用struck.unpack获取子字符串



Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/

-->import struct
   print struct.unpack(format, 'Test astring')
#('Test', 'ing')

来个简单的例子吧,有一个字符串'He is not very happy',处理一下,把中间的not去掉,然后再输出。



Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/

-->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

作者:CoderZhCoderZh的技术博客 - 博客园
微博:http://t.sina.com.cn/coderzh 
出处:http://coderzh.cnblogs.com
文章版权归本人所有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。

Python天天美味(13) - struct.unpack的更多相关文章

  1. Python天天美味(15) - Python正则表达式操作指南(re使用)(转)

    http://www.cnblogs.com/coderzh/archive/2008/05/06/1185755.html 简介 Python 自1.5版本起增加了re 模块,它提供 Perl 风格 ...

  2. Python天天美味(25) - 深入理解yield

    Python天天美味(25) - 深入理解yield - CoderZh - 博客园 Python天天美味(25) - 深入理解yield   yield的英文单词意思是生产,刚接触Python的时候 ...

  3. Python socket编程之二:【struct.pack】&【struct.unpack】

    import struct """通过 socket 的 send 和 recv 只能传输 str 格式的数据""" "" ...

  4. python中struct.pack()函数和struct.unpack()函数

    python中的struct主要是用来处理C结构数据的,读入时先转换为Python的字符串类型,然后再转换为Python的结构化类型,比如元组(tuple)啥的~.一般输入的渠道来源于文件或者网络的二 ...

  5. python 常用的模块(struct)转

    准确地讲,Python没有专门处理字节的数据类型.但由于str既是字符串,又可以表示字节,所以,字节数组=str.而在C语言中,我们可以很方便地用struct.union来处理字节,以及字节和int, ...

  6. struct.pack()和struct.unpack() 详解(转载)

    原文链接:https://blog.csdn.net/weiwangchao_/article/details/80395941 python 中的struct主要是用来处理C结构数据的,读入时先转换 ...

  7. 程序游戏推荐(C语言贪吃蛇,python天天酷跑(需要安装pygame),js是狠人就坚持30s)

    下面是下载位置,我把他们上传到我的文件下了. C语言贪吃蛇:https://files.cnblogs.com/files/ITXiaoAng/%E8%B4%AA%E5%90%83%E8%9B%87. ...

  8. python 天天生鲜项目

    python 天天生鲜项目 django版:https://github.com/Ivy-1996/fresh flask版:https://github.com/Ivy-1996/flask-fre ...

  9. Python中struct.pack()和struct.unpack()

    https://blog.csdn.net/tjuyanming/article/details/79700601 https://www.cnblogs.com/yezl/p/5861787.htm ...

随机推荐

  1. mysql用户备份与修复

    1.修复表repair table tb1 [use frm]; #红色部分代表可添加也可不加, 2.show variables like '%timeout%';  #查询关键字 3. 更改数据, ...

  2. web开发-服务器Controller到前端中的数据传递

    一, ajax方式 (一)controller中 1. 定义AjaxResponse类 成员有: status , message, data.  其中 status是成功或失败状态, message ...

  3. POJ 2528 区间染色,求染色数目,离散化

    Mayor's posters Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 47905   Accepted: 13903 ...

  4. 【个人使用.Net类库】前言

    个人接触.Net是在2013年8月份到了一家新的公司开始的. 目前为止,发现自己的知识储备如下: 基本的WinForm编程,但没做过对应项目. 基本的Asp.Net页面,做过查询.树形菜单.登录的小功 ...

  5. CodeForces 546B C(Contest #1)

    Description Colonel has n badges. He wants to give one badge to every of his n soldiers. Each badge ...

  6. Linux下screen命令

    //1.列出当前的screenscreen -ls //2.新建一个screen,直接在命令行键入screen命令 screen -S [会话名称][root@www.lnuxidc.com ~]# ...

  7. 解决maven Generating project in Interactive mode卡死问题(转)

    原文链接:http://blog.csdn.net/only_wan/article/details/52975760 mvn 创建时在generating project in interactiv ...

  8. 微软TechEd2013大会将在北京、上海召开!

    微软TechEd2013大会将在北京.上海召开 大家期盼已久的微软TechEd2013大会终于到来了! 我公司依旧是微软公司指定票商 ,继续为您提供最最优质的售前咨询.最最完善的售后服务! 微软Tec ...

  9. WPF如何实现类似iPhone界面切换的效果(转载)

    WPF如何实现类似iPhone界面切换的效果 (version .1) 转自:http://blog.csdn.net/fallincloud/article/details/6968764 在论坛上 ...

  10. hdu 2028

    PS:以前对long long型的数据就一直不怎么明白...弄了好久... long long a; scanf("%lld",&a); printf("%lld ...