用于序列化/反序列化的python编码/解码器(python中Java的kyro等价)

时间:2013-05-29 15:47:12

标签: python serialization deserialization

我需要将python值转换为字节数组,反之亦然。

例如:

  1. 整数256 - > [1,1] // 256 = 1 * 255 + 1
  2. 字符串'ab' - > [97,98] //'a' - > 97,'b' - > 98
  3. 浮点1.23 - > [63,157,112,164] // 1.23为4字节表示
  4. 对于java kryo可用于此目的:byte[] serializedValue = kryoSerializer.writeObjectData(value);为我提供了序列化的值结果。

    我尝试了pickle,但我不能使用它,因为它消耗了6个字节来存储整数对象。

    import pickle
    
    Foo = 256
    picklestring = pickle.dumps(Foo)
    print len(picklestring) # returns 6
    

    有任何提示吗?

    ADDED

    # http://docs.python.org/2/library/struct.html
    # http://stackoverflow.com/questions/16818463/python-encode-decoder-for-serialization-deserialization-javas-kyro-equivalence
    # http://stackoverflow.com/questions/11624190/python-convert-string-to-byte-array
    import struct
    
    # >f for 
    def encode(value):
        formatString = ""
        if type(value) is float:
            formatString = ">f"
        elif type(value) is int:
            formatString = ">i"
        elif type(value) is str:
            formatString = ">s"
        else:
            raise Exception("Wrong data input: only supports float/int/string")
        packed = struct.pack(formatString, value)
        result = []
        for i in packed:
            # i is a string
            result.append(ord(i[0]))
        return result
    

1 个答案:

答案 0 :(得分:1)

使用struct module

>>> import struct
>>> struct.pack('>f', 1.23)
'?\x9dp\xa4'
>>> len(struct.pack('>f', 1.23))
4

结构包遵循C约定的值;上面的格式以big-endian顺序打包一个单精度浮点值(4个字节)。