views:

145

answers:

4

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.

A: 

It not clear what you want to do:

  1. Do something with the serialized form of an entire message (From the SerializeAsString method). Not sure what you'd want to do with this?
  2. Store a byte string inside a protobuf message - just use the bytes type in the .proto file, and a byte string in python for the variable.
Douglas Leeder
A: 

I think that strings are the usual way to represent binary data in Python. What do you exactly want to do?

[Edit]

Have a look at the struct module: http://docs.python.org/library/struct.html

Bastien Léonard
I've updated my answer.
nbolton
A: 

You can use Pythons Strings for getting proto buffers serialized data (doesn't matter how they ware crated - in Python, Java, C++ or any other language).

These is line from Pythons version of proto buffers tutorial: address_book.ParseFromString(f.read())

Maciek Sawicki
A: 

Python strings can hold binary data, therefore SerializeAsString returns binary data.

nbolton