Search code examples
pythonprotocol-buffers

Using Python, how do I get a binary serialization of my Google protobuf message?


I see the function SerializeAsString in the protobuf Python documentation, but like this suggests, this gives me a string version of the binary data. Is there a way of serializing and parsing a binary array of protobuf data using Python?

We have a C++ application that stores the protobuf messages as binary data in a file. We'd like to read and write to the file using Python.


Solution

  • Python 2 (edit: end of life 2020) strings can hold binary data, therefore SerializeAsString returns binary data.

    Edit: Surfacing from comments: "In Python 3, the SerializeToString method returns an instance of bytes"