gpt4 book ai didi

python-3.x - 如何从 Python Dict 创建 Protobuf Struct?

转载 作者:行者123 更新时间:2023-12-03 09:54:46 25 4
gpt4 key购买 nike

我收到以下属性错误

AttributeError: 'Struct' object has no attribute 'fields'



如果我想使用 update google.protobuf.internal.well_known_types.Struct 的方法

Protobuf 版本是 3.71。

MWE :
from google.protobuf.internal.well_known_types import Struct

s = Struct()
s.update({"key": "value"})

这个问题的更大背景是我想用 google.protobuf.Struct 创建一条消息。 python 中的字段,用于发送以传递给生成的 RPC 客户端。

任何人都可以帮忙吗?

最佳答案

好的,我在写完问题后立即发现了如何做到这一点。将答案留给可能最终遇到此问题的任何其他人。

我们必须导入 Struct来自 google.protobuf.struct_pb2 .然后update将毫无问题地工作。

因此,

from google.protobuf.struct_pb2 import Struct

s = Struct()
s.update({"key": "value"})

将返回一个具有表示的对象
fields {
key: "key"
value {
string_value: "value"
}
}

关于python-3.x - 如何从 Python Dict 创建 Protobuf Struct?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56303736/

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