python - python protobuf 将字典分配给 google proto 的任何字段
问题描述
我有一个 .prot 文件,其中包含以下字段 user.proto
message Integration {
string db_name = 1;
oneof payload {
Asset asset = 2;
}
}
message Asset {
string address = 1;
google.protobuf.Any extra_fields = 2;
}
我只想为 extra_fields 分配一个大字典,如下所示
导入生成的 pb2 文件
import user_pb2
i = user_pb2.Integration()
i.db_name = "sdsdsd"
i.asset.address = "sdsd"
i.asset.extra_fields = {"assd":"sdsd","sd":"asd"...}
但它正在提高
AttributeError: Assignment not allowed to field "extra_fields" in the protocol message object.
我不想在 proto 中指定文件名,因为我的 dict 包含超过 100 个字段我只想将总 dict 分配给额外的字段有人可以建议如何将 dict 插入额外的字段吗?
解决方案
您只需要跳过.asset
并分配i.address
或i.extra_fields
直接。例如:
i.extra_fields = {"a": "b"}
查看文档:https ://developers.google.com/protocol-buffers/docs/reference/python-generated#oneof
推荐阅读
- javascript - Vue JS 不在 Electron 应用程序内部呈现
- django - 将数据插入两个连接的序列化程序时出错
- oracle - 在结果中添加一列,显示按年份分组的序号
- r - 如何在 R 中读取 Adblock 过滤器
- android - 显示彩色通知图标,例如 Google Pay 和其他一些应用
- javascript - React - 传递给子组件时道具的值未准备好
- ios - iOS 应用程序使用 TWILIO 从应用程序拨打电话
- python - ModuleNotFoundError:django 中没有名为“my_app.urls”的模块
- python - 在使用 python 的数据块中,dbutils.fs.mount 给出 java.lang.NullPointerException: authEndpoint 试图使用 abfss 挂载。wasbs 工作正常
- python - 在 Glue pyspark 中使用参数