Python struct.unpack
更新时间:2008年09月06日 14:07:52 作者:
Python中按一定的格式取出某字符串中的子字符串,使用struck.unpack是非常高效的。
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
复制代码 代码如下:
# 取前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使用psycopg2连接PostgreSQL数据库的步骤
PostgreSQL 是一个广泛使用的开源对象关系数据库系统,以其强大的功能和灵活性而闻名,Python,作为一种流行的编程语言,提供了多种方式与数据库交互,其中 psycopg2 是连接 PostgreSQL 数据库的流行选择之一,本文介绍了Python使用psycopg2连接PostgreSQL数据库的步骤2024-12-12
Tensorflow: 从checkpoint文件中读取tensor方式
今天小编就为大家分享一篇Tensorflow: 从checkpoint文件中读取tensor方式,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧2020-02-02


最新评论