簡體   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