Loading [MathJax]/jax/output/CommonHTML/config.js
前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >专栏 >Python学习——struct模块的pack、unpack示例

Python学习——struct模块的pack、unpack示例

作者头像
阳光岛主
发布于 2019-02-20 08:32:49
发布于 2019-02-20 08:32:49
2.3K00
代码可运行
举报
文章被收录于专栏:米扑专栏米扑专栏
运行总次数:0
代码可运行

import struct

pack、unpack、pack_into、unpack_from

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
# ref: http://blog.csdn.net/JGood/archive/2009/06/22/4290158.aspx

import struct

#pack - unpack
print
print '===== pack - unpack ====='

str = struct.pack("ii", 20, 400)
print 'str:', str
print 'len(str):', len(str) # len(str): 8 

a1, a2 = struct.unpack("ii", str)
print "a1:", a1  # a1: 20
print "a2:", a2  # a2: 400

print 'struct.calcsize:', struct.calcsize("ii") # struct.calcsize: 8


#unpack
print
print '===== unpack ====='

string = 'test astring'
format = '5s 4x 3s'
print struct.unpack(format, string) # ('test ', 'ing')

string = 'he is not very happy'
format = '2s 1x 2s 5x 4s 1x 5s'
print struct.unpack(format, string) # ('he', 'is', 'very', 'happy')


#pack
print
print '===== pack ====='

a = 20
b = 400

str = struct.pack("ii", a, b)
print 'length:', len(str) #length: 8
print str
print repr(str) # '/x14/x00/x00/x00/x90/x01/x00/x00'


#pack_into - unpack_from
print
print '===== pack_into - unpack_from ====='
from ctypes import create_string_buffer

buf = create_string_buffer(12)
print repr(buf.raw)

struct.pack_into("iii", buf, 0, 1, 2, -1)
print repr(buf.raw)

print struct.unpack_from("iii", buf, 0)
                                                   

运行结果:

[work@db-testing-com06-vm3.db01.baidu.com python]$ python struct_pack.py

===== pack - unpack ===== str: ? len(str): 8 a1: 20 a2: 400 struct.calcsize: 8

===== unpack ===== ('test ', 'ing') ('he', 'is', 'very', 'happy')

===== pack ===== length: 8 ? '/x14/x00/x00/x00/x90/x01/x00/x00'

===== pack_into - unpack_from ===== '/x00/x00/x00/x00/x00/x00/x00/x00/x00/x00/x00/x00' '/x01/x00/x00/x00/x02/x00/x00/x00/xff/xff/xff/xff' (1, 2, -1)

==============================================================================

Python是一门非常简洁的语言,对于数据类型的表示,不像其他语言预定义了许多类型(如:在C#中,光整型就定义了8种)

它只定义了六种基本类型:字符串,整数,浮点数,元组(set),列表(array),字典(key/value)

通过这六种数据类型,我们可以完成大部分工作。但当Python需要通过网络与其他的平台进行交互的时候,必须考虑到将这些数据类型与其他平台或语言之间的类型进行互相转换问题。打个比方:C++写的客户端发送一个int型(4字节)变量的数据到Python写的服务器,Python接收到表示这个整数的4个字节数据,怎么解析成Python认识的整数呢? Python的标准模块struct就用来解决这个问题。

struct模块的内容不多,也不是太难,下面对其中最常用的方法进行介绍:

1、 struct.pack struct.pack用于将Python的值根据格式符,转换为字符串(因为Python中没有字节(Byte)类型,可以把这里的字符串理解为字节流,或字节数组)。其函数原型为:struct.pack(fmt, v1, v2, ...),参数fmt是格式字符串,关于格式字符串的相关信息在下面有所介绍。v1, v2, ...表示要转换的python值。下面的例子将两个整数转换为字符串(字节流):

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
#!/usr/bin/env python
#encoding: utf8

import sys
reload(sys)
sys.setdefaultencoding("utf-8")

import struct

a = 20
b = 400 
str = struct.pack("ii", a, b)
print 'length: ', len(str)          # length:  8
print str                           # 乱码: 
print repr(str)                     # '\x14\x00\x00\x00\x90\x01\x00\x00'

格式符"i"表示转换为int,'ii'表示有两个int变量。

进行转换后的结果长度为8个字节(int类型占用4个字节,两个int为8个字节)

可以看到输出的结果是乱码,因为结果是二进制数据,所以显示为乱码。

可以使用python的内置函数repr来获取可识别的字符串,其中十六进制的0x00000014, 0x00001009分别表示20和400。

2、 struct.unpack struct.unpack做的工作刚好与struct.pack相反,用于将字节流转换成python数据类型。它的函数原型为:struct.unpack(fmt, string),该函数返回一个元组。 

下面是一个简单的例子:

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
#!/usr/bin/env python
#encoding: utf8

import sys
reload(sys)
sys.setdefaultencoding("utf-8")

import struct

a = 20
b = 400 

# pack
str = struct.pack("ii", a, b)
print 'length: ', len(str)          # length:  8
print str                           # 乱码: 
print repr(str)                     # '\x14\x00\x00\x00\x90\x01\x00\x00'

# unpack
str2 = struct.unpack("ii", str)
print 'length: ', len(str2)          # length:  2
print str2                           # (20, 400)
print repr(str2)                     # (20, 400)

3、 struct.calcsize struct.calcsize用于计算格式字符串所对应的结果的长度,如:struct.calcsize('ii'),返回8。因为两个int类型所占用的长度是8个字节。

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
import struct
print "len: ", struct.calcsize('i')       # len:  4
print "len: ", struct.calcsize('ii')      # len:  8
print "len: ", struct.calcsize('f')       # len:  4
print "len: ", struct.calcsize('ff')      # len:  8
print "len: ", struct.calcsize('s')       # len:  1
print "len: ", struct.calcsize('ss')      # len:  2
print "len: ", struct.calcsize('d')       # len:  8
print "len: ", struct.calcsize('dd')      # len:  16

4、 struct.pack_into、 struct.unpack_from 这两个函数在Python手册中有所介绍,但没有给出如何使用的例子。其实它们在实际应用中用的并不多。Google了很久,才找到一个例子,贴出来共享一下:

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
#!/usr/bin/env python
#encoding: utf8

import sys
reload(sys)
sys.setdefaultencoding("utf-8")

import struct
from ctypes import create_string_buffer

buf = create_string_buffer(12)
print repr(buf.raw)     # '\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00'

struct.pack_into("iii", buf, 0, 1, 2, -1)
print repr(buf.raw)     # '\x01\x00\x00\x00\x02\x00\x00\x00\xff\xff\xff\xff'

print struct.unpack_from("iii", buf, 0)     # (1, 2, -1)

具体内容请参考Python手册 struct 模块

Python手册 struct 模块:http://docs.python.org/library/struct.html#module-struct

struct 类型表

Format

C Type

Python type

Standard size

Notes

x

pad byte

no value

c

char

string of length 1

1

b

signed char

integer

1

(3)

B

unsigned char

integer

1

(3)

?

_Bool

bool

1

(1)

h

short

integer

2

(3)

H

unsigned short

integer

2

(3)

i

int

integer

4

(3)

I

unsigned int

integer

4

(3)

l

long

integer

4

(3)

L

unsigned long

integer

4

(3)

q

long long

integer

8

(2), (3)

Q

unsigned long long

integer

8

(2), (3)

f

float

float

4

(4)

d

double

float

8

(4)

s

char[]

string

1

p

char[]

string

P

void *

integer

(5), (3)

Notes:

  1. The '?' conversion code corresponds to the _Bool type defined by C99. If this type is not available, it is simulated using a char. In standard mode, it is always represented by one byte. New in version 2.6.
  2. The 'q' and 'Q' conversion codes are available in native mode only if the platform C compiler supports C long long, or, on Windows, __int64. They are always available in standard modes. New in version 2.2.
  3. When attempting to pack a non-integer using any of the integer conversion codes, if the non-integer has a __index__() method then that method is called to convert the argument to an integer before packing. If no __index__() method exists, or the call to __index__() raises TypeError, then the __int__() method is tried. However, the use of __int__() is deprecated, and will raise DeprecationWarning. Changed in version 2.7: Use of the __index__() method for non-integers is new in 2.7. Changed in version 2.7: Prior to version 2.7, not all integer conversion codes would use the __int__() method to convert, and DeprecationWarning was raised only for float arguments.
  4. For the 'f' and 'd' conversion codes, the packed representation uses the IEEE 754 binary32 (for 'f') or binary64 (for 'd') format, regardless of the floating-point format used by the platform.
  5. The 'P' format character is only available for the native byte ordering (selected as the default or with the '@' byte order character). The byte order character '=' chooses to use little- or big-endian ordering based on the host system. The struct module does not interpret this as native ordering, so the 'P' format is not available.

A format character may be preceded by an integral repeat count. For example, the format string '4h' means exactly the same as 'hhhh'.

Whitespace characters between formats are ignored; a count and its format must not contain whitespace though.

For the 's' format character, the count is interpreted as the size of the string, not a repeat count like for the other format characters; for example, '10s' means a single 10-byte string, while '10c' means 10 characters. For packing, the string is truncated or padded with null bytes as appropriate to make it fit. For unpacking, the resulting string always has exactly the specified number of bytes. As a special case, '0s' means a single, empty string (while '0c' means 0 characters).

The 'p' format character encodes a “Pascal string”, meaning a short variable-length string stored in a fixed number of bytes, given by the count. The first byte stored is the length of the string, or 255, whichever is smaller. The bytes of the string follow. If the string passed in to pack() is too long (longer than the count minus 1), only the leading count-1 bytes of the string are stored. If the string is shorter than count-1, it is padded with null bytes so that exactly count bytes in all are used. Note that for unpack(), the 'p' format character consumes count bytes, but that the string returned can never contain more than 255 characters.

For the 'P' format character, the return value is a Python integer or long integer, depending on the size needed to hold a pointer when it has been cast to an integer type. A NULL pointer will always be returned as the Python integer 0. When packing pointer-sized values, Python integer or long integer objects may be used. For example, the Alpha and Merced processors use 64-bit pointer values, meaning a Python long integer will be used to hold the pointer; other platforms use 32-bit pointers and will use a Python integer.

For the '?' format character, the return value is either True or False. When packing, the truth value of the argument object is used. Either 0 or 1 in the native or standard bool representation will be packed, and any non-zero value will be True when unpacking.

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2010年10月29日,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
暂无评论
推荐阅读
编辑精选文章
换一批
Python中struct.pack()和struct.unpack()用法
python中的struct主要是用来处理C结构数据的,读入时先转换为Python的字符串类型,然后再转换为Python的结构化类型,比如元组(tuple)啥的~。一般输入的渠道来源于文件或者网络的二进制流。
周小董
2019/03/25
16.7K2
Python中struct.pack()和struct.unpack()用法
python网络-TFTP客户端开发(25)
TFTP(Trivial File Transfer Protocol,简单文件传输协议)
Se7eN_HOU
2019/09/11
2.4K0
python网络-TFTP客户端开发(25)
python学习笔记7.5-内建模块struct
Python中变量的类型只有列表、元祖、字典、集合等高级抽象类型,并没有像c中定义了位、字节、整型等底层初级类型。因为Python本来就是高级解释性语言,运行的时候都是经过翻译后再在底层运行。如何打通
锦小年
2018/01/02
7590
一文学会Python标准库struct序列化与反序列化
使用Python标准库struct序列化Python整数、实数、字节串时,需要使用struct模块的pack()函数把对象按指定的格式进行序列化,然后使用文件对象的write()方法将序列化的结果字节串写入以'wb'或'ab'模式打开的二进制文件。读取时需要使用文件对象的read()方法从以'rb'模式打开的二进制文件中读取指定数量的字节串,然后再使用struct模块的unpack()函数反序列化得到原来对象息。如果需要的话,可以使用calcsize()函数计算指定类型序列化时所需要的字节数量。标准库struct中常用的函数及功能下表所示。
Python小屋屋主
2020/02/24
1.4K0
Python基础之:struct和格式化字符
文件的存储内容有两种方式,一种是二进制,一种是文本的形式。如果是以文本的形式存储在文件中,那么从文件中读取的时候就会遇到一个将文本转换为Python中数据类型的问题。实际上即使是文本的形式存储,存储的数据也是也是有结构的,因为Python底层是用C来编写的,这里我们也称之为C结构。
程序那些事
2021/04/16
9350
Python之struct
  (3) 处理二进制数据,如果用struct来处理文件的话,需要用‘wb’/’rb’以二进制写,读的方式来处理文件
全栈程序员站长
2022/07/14
4960
2018-06-30 Python Struct
Python使用struct处理二进制 例如: import struct a = 20 b = 400 s = struct.pack('ii', a, b) print(s, type(s)) #输出:b'\x14\x00\x00\x00\x90\x01\x00\x00' print('length: ', len(s)) #输出:length:  8 s2 = struct.unpack('ii', s) print(s2) #输出:(20, 400) s2 = struct.unpack('ii',
用户1733354
2018/07/05
5400
Python中对字节流/二进制流的操作:struct模块简易使用教程
前言 前段时间使用Python解析IDX文件格式的MNIST数据集,需要对二进制文件进行读取操作,其中我使用的是struct模块。查了网上挺多教程都写的挺好的,不过对新手不是很友好,所以我重新整理了一些笔记以供快速上手。 注:教程中以下四个名词同义:二进制流、二进制数组、字节流、字节数组 快速上手 在struct模块中,将一个整型数字、浮点型数字或字符流(字符数组)转换为字节流(字节数组)时,需要使用格式化字符串fmt告诉struct模块被转换的对象是什么类型,比如整型数字是'i',浮点型数字是'f',一个
用户1332428
2018/03/09
3K0
Python中对字节流/二进制流的操作:struct模块简易使用教程
使用python 实现icmp测试主机存
import os, sys, socket, struct, select, time
py3study
2020/01/11
8780
python| struct 模块
最近研究了一下python 解析MySQL binlog 文件的内容,binlog是二进制存储,python如何解析成我们能读懂的语言呢?答案就是 struct 模块用于 Python 值和用 Python 字节对象表示的 C 结构体之间的转换,可以处理存储在文件,网络或者其他数据源的二进制数据。
用户1278550
2020/06/23
6780
用struct模块实现python so
最近跳槽到西安一家机器人公司,我们的产品属于教育机器人的范畴,为了增强客户吸引力,引进了一个智能家居公司的产品API接口,让机器人来操作智能家居
py3study
2020/01/07
6230
python struct模块_Python struct模块
有的时候需要用python处理二进制数据,比如,存取文件,socket操作时.这时候,可以使用python的struct模块来完成.可以用 struct来处理c语言中的结构体.
用户7886150
2020/12/22
1.6K0
pkg文件--一种简单的游戏资源打包格式
[四字节] 固定的内容, 值不重要  [四字节] 文件数目(unsigned int)  [四字节] 文件名表 的偏移(unsigned int)  [四字节] 文件名表 的长度(字节数)(unsigned int)  ……  中间一堆 各个文件的内容, 文件内容使用zlib压缩过  ……  直到  文件名表:  [两字节] 文件名长度  [文件名长度那么多字节] 文件名  [四字节] 固定的内容,值不重要  [四字节] 文件原长度  [四字节] 文件偏移  [四字节] 文件压缩后的长度  [两字节] 又一个文件名的长度  …
用户7886150
2020/11/23
2.2K0
Python之struct简介
       看到struct这么英文单词,大家应该并不陌生,因为c/c++中就有struct,在那里struct叫做结构体。在Python中也使用struct,这充分说明了这个struct应该和c/c++中的struct有很深的渊源。Python正是使用struct模块执行Python值和C结构体之间的转换,从而形成Python字节对象。它使用格式字符串作为底层C结构体的紧凑描述,进而根据这个格式字符串转换成Python值。
py3study
2020/01/09
1.8K0
Python 字符串与字节数组转换
整数之间的进制转换: 10进制转16进制: hex(16) ==> 0x10 16进制转10进制: int('0x10', 16) ==> 16 类似的还有oct(), bin() 字符串转整数: 10进制字符串: int('10') ==> 10 16进制字符串: int('10', 16) ==> 16 16进制字符串: int('0x10', 16) ==> 16 字节串转整数: 转义为short型整数: struct.unpack('<hh', bytes(b'\x01\x00\
王 瑞
2022/12/28
1.2K0
python内置函数3-dir()
Help on built-in function dir in module __builtin__:
py3study
2020/01/06
6370
面向对象之套接字(socket)和黏包
 一丶套接字(socket)   tcp是基于链接的,必须先启动服务端,然后再启动客户端去链接服务端   基于UDP协议的socket   server端: import socket udp_sk = socket.socket(type=socket.SOCK_DGRAM) #创建一个服务器的套接字 udp_sk.bind(('127.0.0.1',9000)) #绑定服务器套接字 msg,addr = udp_sk.recvfrom(1024) print(msg) udp_sk.s
py3study
2020/01/19
5960
面向对象之套接字(socket)和黏包
蓝队的反制(2)
 还有几天,某大型全国性交友活动就要开始了,特此为蓝队的朋友们献上一篇简单的反制文章。
鸿鹄实验室
2021/04/01
9020
蓝队的反制(2)
python常用的十进制、16进制、字符串、字节串之间的转换
进行协议解析时,总是会遇到各种各样的数据转换的问题,从二进制到十进制,从字节串到整数等等
用户1214487
2018/07/31
7.5K0
[oeasy]python0019_ 打包和解包_struct_pack_unpack
​打包和解包回忆上次内容ASCII 由这样几类字符构成英文大写字符英文小写字符数字符号电报时代对于英文、数字的编码使用的是摩斯电码​编辑这摩斯电码是3进制的编码方式长短空怎么演化成ascii这种0101的二进制编码的呢?🤔回到 ASCII 码电报传过来的信号需要记录下来于是有了电传打字机(tele-typewriter)电传打字机需要统一的编码字母和数字ASCII的第一个商业用途是作为一个七位电传打字机(tele-typewriter)代码​编辑要把滴、答、停的电报信号变成0和1的二进制信号ASCI
oeasy
2022/11/23
8110
[oeasy]python0019_ 打包和解包_struct_pack_unpack
相关推荐
Python中struct.pack()和struct.unpack()用法
更多 >
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档
本文部分代码块支持一键运行,欢迎体验
本文部分代码块支持一键运行,欢迎体验