您的位置:首页 > 编程语言 > Python开发

【脚本语言系列】关于Python操作数据二进制数据,你需要知道的事情

2017-06-26 15:53 1026 查看
【脚本语言系列】关于Python数据处理二进制数据,你需要知道的事

如何使用二进制数据

字节和字节数组

# -*- coding:utf-8 -*-
# only for Python3.*
blist =[12,23,34,45]
# variable
this_byte_array = bytearray(blist)
print this_byte_array
this_byte_array[1] = 127
print this_byte_array

# invariable
this_bytes = bytes(blist)
print this_bytes
this_bytes[1] = 127
print this_bytes


"-
"-
[12, 23, 34, 45]

---------------------------------------------------------------------------

TypeError                                 Traceback (most recent call last)

<ipython-input-21-9bbd1152090e> in <module>()
11 this_bytes = bytes(blist)
12 print this_bytes
---> 13 this_bytes[1] = 127
14 print this_bytes

TypeError: 'str' object does not support item assignment


# -*- coding:utf-8 -*-
# only for Python3.*
the_bytes = bytes(range(0, 256))
print the_bytes

the_byte_array = bytearray(range(0, 256))
print the_byte_array


[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 110, 111, 112, 113, 114, 115, 116, 117, 118, 119, 120, 121, 122, 123, 124, 125, 126, 127, 128, 129, 130, 131, 132, 133, 134, 135, 136, 137, 138, 139, 140, 141, 142, 143, 144, 145, 146, 147, 148, 149, 150, 151, 152, 153, 154, 155, 156, 157, 158, 159, 160, 161, 162, 163, 164, 165, 166, 167, 168, 169, 170, 171, 172, 173, 174, 175, 176, 177, 178, 179, 180, 181, 182, 183, 184, 185, 186, 187, 188, 189, 190, 191, 192, 193, 194, 195, 196, 197, 198, 199, 200, 201, 202, 203, 204, 205, 206, 207, 208, 209, 210, 211, 212, 213, 214, 215, 216, 217, 218, 219, 220, 221, 222, 223, 224, 225, 226, 227, 228, 229, 230, 231, 232, 233, 234, 235, 236, 237, 238, 239, 240, 241, 242, 243, 244, 245, 246, 247, 248, 249, 250, 251, 252, 253, 254, 255]
!"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcdefghijklmnopqrstuvwxyz{|}


使用struct转换二进制数据

# -*- coding:utf-8 -*-
import struct
valid_png_header = b'\x89PNG\r\n\x1a\n'
data = b'\x89PNG\r\n\x1a\n\x00\x00\x00\rIHDR'+ \
b'\x00\x00\x00\x9a\x00\x00\x00\x8d\x08\x02\x00\x00\x00\xc0'
if data[:8] == valid_png_header:
width,height = struct.unpack('>LL', data[16:24])
print("Valid PNG, widht", width, "height", height)
else:
print("Not a valid PNG")
print data[16:20]
print data[20:24]
print 0x9a
print 0x8d
print struct.unpack('>2L', data[16:24])
print struct.unpack('>16x2L6x', data)


('Valid PNG, widht', 154, 'height', 141)
154
141
(154, 141)
(154, 141)


其他二进制数据工具

bitstring

construct

# -*- coding:utf-8 -*-
# only for Python3.*
from construct import Struct, Magic, UBInt32, Const, String
fmt = Struct('png',
Magic(b'\x89PNG\r\n\x1a\n'),
UBInt32('length'),
Const(String('type', 4), b'IHDR'),
UBInt32('width'),
UBInt32('height'))
data = b'\x89PNG\r\n\x1a\n\x00\x00\x00\rIHDR'+\
b'\x00\x00\x00\x9a\x00\x00\x00\x8d\x08\x02\x00\x00\x00\xc0'
result = fmt.parse(data)
print result


---------------------------------------------------------------------------

ImportError                               Traceback (most recent call last)

<ipython-input-20-53f891107532> in <module>()
1
2 # only for Python3.*
----> 3 from construct import Struct, Magic, UBInt32, Const, String
4 fmt = Struct('png',
5              Magic(b'\x89PNG\r\n\x1a\n'),

ImportError: cannot import name Magic


hachoir

binio

使用binascii()转换字节/字符串

# -*- coding:utf-8 -*-
import binascii
valid_png_header = b'\x89PNG\r\n\x1a\n'
print binascii.hexlify(valid_png_header)
print binascii.unhexlify(b'89504e470d0a1a0a')


89504e470d0a1a0a
�PNG

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
相关文章推荐