Python protobuf SerializeToString() 数据的 UTF-8 解码错误
Python UTF-8 decode error for protobuf SerializeToString() data
我需要 post 一些 protobuf 数据到 API 端点。在进行 API 调用之前,我必须先对其进行解码,但是当版本 int 包含超过 1 个数字时解码失败。这有效:
appinfo = app.InstalledAppInfo()
appinfo.name = str(name)
appinfo.version = 1
data = appinfo.SerializeToString().decode("utf-8")
但是当我尝试使用更大的数字时,它失败了:
appinfo = app.InstalledAppInfo()
appinfo.name = str(name)
appinfo.version = 1342424242
data = appinfo.SerializeToString().decode("utf-8")
这正是我得到的错误:
'utf-8' codec can't decode byte 0x84 in position 25: invalid start byte
我可能做错了什么?
Protobuf数据不是utf-8;你不能 "decode" 它作为 utf-8, 因为它不是 。
如果您需要通过仅支持文本的传输方式发送:base-64 是您的最佳选择。但是如果你能把它作为二进制发送,那会更有效率。
我需要 post 一些 protobuf 数据到 API 端点。在进行 API 调用之前,我必须先对其进行解码,但是当版本 int 包含超过 1 个数字时解码失败。这有效:
appinfo = app.InstalledAppInfo()
appinfo.name = str(name)
appinfo.version = 1
data = appinfo.SerializeToString().decode("utf-8")
但是当我尝试使用更大的数字时,它失败了:
appinfo = app.InstalledAppInfo()
appinfo.name = str(name)
appinfo.version = 1342424242
data = appinfo.SerializeToString().decode("utf-8")
这正是我得到的错误:
'utf-8' codec can't decode byte 0x84 in position 25: invalid start byte
我可能做错了什么?
Protobuf数据不是utf-8;你不能 "decode" 它作为 utf-8, 因为它不是 。
如果您需要通过仅支持文本的传输方式发送:base-64 是您的最佳选择。但是如果你能把它作为二进制发送,那会更有效率。