当前位置: 移动技术网 > IT编程>脚本编程>Python > Python struct.unpack

Python struct.unpack

2019年04月05日  | 移动技术网IT编程  | 我要评论

幽默短信笑话,zt3,我是唐僧他爸

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

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网