Home > Backend Development > Python Tutorial > Python struct.unpack

Python struct.unpack

WBOY
Release: 2016-06-16 08:47:38
Original
1781 people have browsed it

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
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template