用java序列化多个protobuf消息,并用Python对它们进行反序列化

2024-06-08 13:32:16 发布

您现在位置:Python中文网/ 问答频道 /正文

我想在一个文件中存储一堆protobuf消息,然后再阅读它们。在

在java中,我可以使用'writeDelimitedTo'和'parseDelimitedFrom'来读写文件。不过,我想在Python中阅读它,它似乎只有一个“ParseFromString”方法。在

有些SO问题非常相似,例如,Parsing Protocol Buffers, written in Java and read in Python,但这仅适用于单个消息:而不是多个消息。在


Tags: 文件方法in消息sojavaprotocolparsing
1条回答
网友
1楼 · 发布于 2024-06-08 13:32:16

从原型指南中可以看出,你需要自己处理信息的大小:

Streaming Multiple Messages

If you want to write multiple messages to a single file or stream, it is up to you to keep track of where one message ends and the next begins. The Protocol Buffer wire format is not self-delimiting, so protocol buffer parsers cannot determine where a message ends on their own. The easiest way to solve this problem is to write the size of each message before you write the message itself. When you read the messages back in, you read the size, then read the bytes into a separate buffer, then parse from that buffer. (If you want to avoid copying bytes to a separate buffer, check out the CodedInputStream class (in both C++ and Java) which can be told to limit reads to a certain number of bytes.)

https://developers.google.com/protocol-buffers/docs/techniques

一个简单的解决方案是在base64中,在文件的新行上序列化每个proto。在

这样,python就可以很容易地解析和使用它们。在

相关问题 更多 >