加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Python > 正文

Python struct.unpack

发布时间:2020-12-17 07:24:18 所属栏目:Python 来源:网络整理
导读:1. 设置fomat格式,如下: 复制代码 代码如下: # 取前5个字符,跳过4个字符华,再取3个字符 format = '5s 4x 3s' 2. 使用struck.unpack获取子字符串 复制代码 代码如下: import struct print struct.unpack(format,'Test astring') #('Test','ing') 来个简单的
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将数组的元素导出到变量中(unpacking)
  • Python中的pack和unpack的使用

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读