首页 > 编程语言 > 详细

Python struct.unpack

时间:2016-01-31 18:50:33      阅读:275      评论:0      收藏: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

Python struct.unpack

原文:http://www.jb51.net/article/15705.htm

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!