Tuesday, April 21, 2009

Write a text format protobuf in python

The protobuf docs aren't clear on this, but I found the magic incantation in one of the tests.

To create a text format (ascii printable) protocol buffer using the python interface,

from google.protobuf import text_format

text_format.MessageToString(your_protobuf_object)

1 comment:

kenton said...

Note that you can also just do str(message_object) to get the same result. The interesting part about the text_format module is that it can also parse this format back into a message object (as of v2.2.0), and the C++ and Java runtimes also support parsing and writing the same format via their TextFormat classes. Works great for human-editable config files.