Google Protocol Buffers (protobuf) in Python3 - trouble with ParseFromString (encoding?)

允我心安 提交于 2019-12-09 13:01:32

问题


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:


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


来源:https://stackoverflow.com/questions/33816761/google-protocol-buffers-protobuf-in-python3-trouble-with-parsefromstring-en

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!