大家好,又见面了,我是全栈君。
1. 设置fomat格式,如下:

#
取前5个字符,跳过4个字符,再取3个字符

format
=
‘
5s 4x 3s
‘
2. 使用struck.unpack获取子字符串

import
struct
struct.unpack(format,
‘
Test astring
‘
)

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

import
struct

=
‘
He is not very happy
‘

=
‘
2s 1x 2s 5x 4s 1x 5s
‘

‘
‘
.join(struct.unpack(format, theString))
输出结果:
He is very happy
Python 天天美味系列(总)
Python 天天美味(11) – 可爱的大小写
Python 天天美味(12) – 条件判断的缩写
Python 天天美味(13) – struct.unpack
Python 天天美味(15) – Python正则表达式操作指南(re使用)(转)
…
本文转自CoderZh博客园博客,原文链接:http://www.cnblogs.com/coderzh/archive/2008/05/04/1181462.html,如需转载请自行联系原作者
发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/108688.html原文链接:https://javaforall.net