gpt4 book ai didi

python-3.x - Python3 中的 Google Protocol Buffer (protobuf) - ParseFromString(编码?)

转载 作者:行者123 更新时间:2023-12-04 17:17:18 37 4
gpt4 key购买 nike

我有 80% 的 Google Protocol Buffer 在 Python3 中工作。我的 .proto 文件有效,我正在编码数据,生活几乎是美好的。
问题是我不能 ParseFromString SerializeToString 的结果。
当我打印 SerializeToString 时,它看起来像我期望的那样,一个相当紧凑的二进制表示(以 b' 开头)。

我的猜测是,这可能是 Python2 和 Python3 处理字符串的方式不同。 SerializeToString 的输入是字节,而不是字符串。

SerializeToString 的打印输出(Python 类型为 ):

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) 的结果:
None

没有提供错误...

所以 - 我最好的猜测是我需要做的就是 .decode() 生成的字节对象,问题是我不知道编码是什么。我试过 UTF-8、-16、Latin-1 和其他一些没有成功。我的 Google-Fu 很强大,但我没有找到任何关于此的信息。

任何帮助,将不胜感激。

最佳答案

ParseFromString是一个方法——它不返回任何东西,而是填入 self与解析的内容。像这样使用它:

message = MyMessageType()
message.ParseFromString(data)
print message.some_field

关于python-3.x - Python3 中的 Google Protocol Buffer (protobuf) - ParseFromString(编码?),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33816761/

37 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com