如何使用python解析protobuf bytearray
我不熟悉protobuf的概念。我正在尝试读取一个用protobuf编码编写的文件。这个文件是用java编写的,我正试图用python来阅读它如何使用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 =
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);