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

来源:互联网 发布:软件数据接口 编辑:程序博客网 时间:2024/05/16 10:24

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

如何使用二进制数据

字节和字节数组

# -*- coding:utf-8 -*-# only for Python3.*blist =[12,23,34,45]# variablethis_byte_array = bytearray(blist)print this_byte_arraythis_byte_array[1] = 127print this_byte_array# invariablethis_bytes = bytes(blist)print this_bytesthis_bytes[1] = 127print 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_bytesTypeError: 'str' object does not support item assignment
# -*- coding:utf-8 -*-# only for Python3.*the_bytes = bytes(range(0, 256))print the_bytesthe_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 structvalid_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 0x9aprint 0x8dprint struct.unpack('>2L', data[16:24])print struct.unpack('>16x2L6x', data)
('Valid PNG, widht', 154, 'height', 141)154141(154, 141)(154, 141)

其他二进制数据工具

  • bitstring
  • construct
# -*- coding:utf-8 -*-# only for Python3.*from construct import Struct, Magic, UBInt32, Const, Stringfmt = 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 binasciivalid_png_header = b'\x89PNG\r\n\x1a\n'print binascii.hexlify(valid_png_header)print binascii.unhexlify(b'89504e470d0a1a0a')
89504e470d0a1a0a�PNG
阅读全文
0 0
原创粉丝点击