I couldn't find anywhere a possibility to print a human friendly content of a Google Protobuf message.
Is there an equivalent in Python for Java's toString()
or C++'s DebugString()
?
I couldn't find anywhere a possibility to print a human friendly content of a Google Protobuf message.
Is there an equivalent in Python for Java's toString()
or C++'s DebugString()
?
If you're using the protobuf package, the
print
function/statement will give you a human-readable representation of the message, because of the__str__
method :-).As answered,
print
and__str__
do work, but I wouldn't use them for anything more than debug strings.If you're writing to something that users could see, it's better to use the
google.protobuf.text_format
module, which has some more controls (e.g. escaping UTF8 strings or not), as well as functions for parsing text-format as protobufs.Here's an example for read/write human friendly text file using
protobuf 2.0
in python.read from a text file
write to a text file
The c++ equivalent is: