Google Protocol Buffers (protobuf) in Python3 - tr

2019-03-31 00:31发布

I've got Google Protocol buffers 80% working in Python3. My .proto file works, I'm encoding data, life is almost good. The problem is that I can't ParseFromString the result of SerializeToString. When I print SerializeToString it looks like what I'd expect, a fairly compact binary representation (preceded by b').

My guess is that perhaps this is a difference in the way Python2 and Python3 handle strings. The putput of SerializeToString is Bytes, not a string.

Printed output of SerializeToString (Python type is ):

b'\x10\xd7\xeb\x8e\xcd\x04\x1a\x0cnamegoeshere2@\x08\x80\xf8\xde\xc3\x9f\xb0\x81\x89\x14\x11\x00\x00\x00\x00\x00\x80d\xc0\x19\x00\x00\x00\x00\x00\xc0m@!\x00\x00\x00\x00\x00\x80R\xc0)\x00\x00\x00\x00\x00x\xb7\xc01\x00\x00\x00\x00\x00\x8c\x95@9\x00\x00\x00\x00\x00\x16\xb2@'

result of ParseFromString(message):

None

No error is provided...

So - my best guess is that all I need to do is .decode() the bytes object generated, the problem is that I have no clue what the encoding is. I've tried UTF-8, -16, Latin-1, and a few others without success. My Google-Fu is strong but I haven't found anything on this.

Any help would be appreciated.

1条回答
孤傲高冷的网名
2楼-- · 2019-03-31 01:19

ParseFromString is a method -- it does not return anything, but rather fills in self with the parsed content. Use it like:

message = MyMessageType()
message.ParseFromString(data)
print message.some_field
查看更多
登录 后发表回答