繁体   English   中英

如何递归遍历 Protobuf Python 消息以查找所有字段

[英]How to iterate recursively over Protobuf Python message to find all field

我就是这样做的,是否有一种本地方法可以在嵌套的 protobuf 消息中查找所有字段;

这是一个两层嵌套消息

for field in mes2.DESCRIPTOR.fields:
  if 'fields' in dir(field.message_type):
    for sub_field in field.message_type.fields:
      print(sub_field)  

你是这个意思?

message = [1, 7, {3: "tsdf", "y": (4, 7)}, [4, 8, 'w']]

def print_fields(message):
    if type(message) not in (list, tuple, dict, set):
        return
    for i in message:
        if type(i) in (list, tuple, dict, set):
            print_fields(i)
        else:
            print(i)

# -> 1, 7, 3, 'y', 4, 8, 'w'

暂无
暂无

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM