Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/293.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何使用python解析protobuf bytearray_Python_Arrays_Protocol Buffers - Fatal编程技术网

如何使用python解析protobuf bytearray

如何使用python解析protobuf bytearray,python,arrays,protocol-buffers,Python,Arrays,Protocol Buffers,我不熟悉protobuf的概念。我正在尝试读取一个用protobuf编码编写的文件。这个文件是用java编写的,我正试图用python来阅读它 with open('out.bin', 'rb') as f: buf = f.read() n = 0 while n < len(buf): msg_len, new_pos = _DecodeVarint32(buf, n) n = new_pos msg_buf =

我不熟悉protobuf的概念。我正在尝试读取一个用protobuf编码编写的文件。这个文件是用java编写的,我正试图用python来阅读它

with open('out.bin', 'rb') as f:
    buf = f.read()
    n = 0
    while n < len(buf):
        msg_len, new_pos = _DecodeVarint32(buf, n)
        n = new_pos
        msg_buf = buf[n:n+msg_len]
        n += msg_len
        read_metric = metric_pb2.Metric()
        read_metric.ParseFromString(msg_buf)
        # do something with read_metric
在java中,以下方法有效:

Serializer<ArrayList<metric_pb2.Metric()>> metricSerializer = new Serializer<>();
result = metricSerializer.fromByteArray(bytearraymessage);
Serializer metricSerializer=new Serializer();
结果=metricSerializer.fromByteArray(ByteArray消息);
但我无法在python中找到如何做到这一点。提前谢谢你的帮助

Serializer<ArrayList<metric_pb2.Metric()>> metricSerializer = new Serializer<>();
result = metricSerializer.fromByteArray(bytearraymessage);