简体   繁体   English

Python 中的协议缓冲区:如何为嵌套元素设置值

[英]Protocol buffer in Python: How to set value to the nested element

i'm having problem while trying to set the value of the nested element in Protobuf file with Python.我在尝试使用 Python 设置 Protobuf 文件中嵌套元素的值时遇到问题。 I have the following protobuf:我有以下protobuf:

syntax = "proto3";

option java_multiple_files = true;

message OuterLayer{
    InnerLayer sim_card_data = 1;
    string version_number = 3;

    message InnerLayer{
        string iccid = 1;
        string imei = 2;
    }

In Python, i set the value by using:在 Python 中,我使用以下方法设置值:

raw = OuterLayer()
raw.version_number = "1.0"
raw.InnerLayer.iccid="1"
raw.InnerLayer.imei="2"

By printing the raw class print(raw) i got only:通过打印原始类 print(raw) 我只得到:

version_number: "1"

The values of the Innerlayer seems not to be set.内层的值似乎没有设置。 What am I doing wrong ?我究竟做错了什么 ? Can anybody help me ?有谁能够帮助我 ?

InnerLayer is the class name not the parameter name so doing the following should work InnerLayer是类名而不是参数名,因此执行以下操作应该可以

raw = OuterLayer()
raw.version_number = "1.0"
raw.sim_card_data = InnerLayer()
raw.sim_card_data.iccid = "1"
raw.sim_card_data.imei = "2"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Python和协议缓冲区:如何从重复的消息字段中删除元素 - Python and protocol buffer: how to removed an element from a repeated message field 如何为协议缓冲区Python中的嵌入消息字段赋值 - How to assign a value to an embedded message field in Protocol buffer Python 有没有办法从嵌套字典初始化协议缓冲区? (在 Python 中) - Is there a way to initialize a Protocol Buffer from a nested dictionary? (in Python) 如何将字典中的值专门设置为 python 上的元素? - How to specifically set a value in a dictionary as an element on python? 在 Python 中读取协议缓冲区文件 - Reading a protocol buffer file in Python 如何使用谷歌协议缓冲区的python-trio? - How to use python-trio with google protocol buffer? 如何为 python 函数中的所有协议缓冲区对象添加类型提示? - How to add type hint for all protocol buffer objects in python functions? 如何从另一个Python包中导入协议缓冲区定义? - How to import protocol buffer definitions from another Python package? 如何设置给定列表位置的嵌套python dict的“第n”元素? - How to set “nth” element of a nested python dict with given list location? 如何在python的嵌套列表中重写某个元素的值? - How to rewrite the value of a certain element in a nested list in python?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM