解析协议缓冲区,用 Java 编写并用 Python 读取 [英] Parsing Protocol Buffers, written in Java and read in Python
问题描述
我正在处理协议缓冲区已用 Java 写入的情况,但我必须使用 Python 读取它们.
I am dealing with a situation where the protocol buffers have been written to in Java, but I have to read them using Python.
我有 .proto 文件,我使用 protoc 编译它,并生成了 python 类文件.
I have the .proto file with me, I have compiled it using protoc, and generated the python class file.
proto 文件有多条消息,例如:
The proto file has multiple messages, for example:
message ABC {
optional int64 id = 1 [default = -1];
optional int64 hello6 = 2;
optional bool hello5 = 3 [default = true];
optional int32 hello4 = 4;
optional int64 hello3 = 5;
optional int64 hello2 = 6;
optional int64 duration = 7;
}
message DEF {
optional int64 hello = 1;
repeated ABC abc = 5;
repeated String xyz = 6;
}
使用 .ParseFromString 在 python 中解析时.
While parsing in python using .ParseFromString.
我收到错误:
google.protobuf.message.DecodeError:标签的连线类型无效.
google.protobuf.message.DecodeError: Tag had invalid wire type.
我认为问题是因为没有明确的分隔符,知道如何解决吗?
I think the issue is because of absence of explicit delimiters, any idea how to solve it?
推荐答案
我遇到了类似的问题,这行得通:
I had similar issue for me this worked:
在 Java 中:
yourGeneratedClass.toByteArray()
在 Python 中你这样做(结果中的解析消息):
In Python you do(parsed message in result):
result = your_pb2.yourGeneratedClass()
result.ParseFromString(body)
这篇关于解析协议缓冲区,用 Java 编写并用 Python 读取的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!