Python3中的Google协议缓冲区(protobuf)-ParseFromString的问题(编码?) [英] Google Protocol Buffers (protobuf) in Python3 - trouble with ParseFromString (encoding?)
问题描述
我有80%的Google协议缓冲区可在Python3中使用。我的.proto文件可以正常工作,我正在对数据进行编码,寿命差不多。
问题是我无法ParseFromString SerializeToString的结果。
当我打印SerializeToString时,它看起来像我期望的那样,是一个相当紧凑的二进制表示形式(以b'开头)。
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').
我的猜测是,也许这是Python2和Python3处理字符串的方式有所不同。
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.
SerializeToString的打印输出(Python类型为):
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@'
ParseFromString(message)的结果:
result of ParseFromString(message):
None
没有错误...
所以-我最好的猜测是,我需要做的只是生成的字节对象.decode(),问题是我不知道编码是什么。我尝试使用UTF-8,-16,Latin-1和其他一些方法均未成功。我的Google-Fu很坚固,但是我还没有找到任何东西。
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.
任何帮助将不胜感激。
推荐答案
ParseFromString
是一种方法-它不返回任何内容,而是使用已解析的内容填充 self
。像这样使用它:
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
这篇关于Python3中的Google协议缓冲区(protobuf)-ParseFromString的问题(编码?)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!