在python中探索Google protobuf

时间:2019-06-19 09:39:16

标签: python protocol-buffers

从dialogflows API中,我收到了一个protobuf。 即使阅读完所有文档,我也不知道如何探索对象并提取值(例如,从其中提取字符串)

我告诉你我做了什么:

(Pdb) dir(parameter)
['ByteSize', 'Clear', 'ClearExtension', 'ClearField', 'CopyFrom', 'DESCRIPTOR', 'DiscardUnknownFields', 'Extensions', 'FieldsEntry', 'FindInitializationErrors', 'FromString', 'HasExtension', 'HasField', 'IsInitialized', 'ListFields', 'MergeFrom', 'MergeFromString', 'ParseFromString', 'RegisterExtension', 'SerializePartialToString', 'SerializeToString', 'SetInParent', 'UnknownFields', 'WhichOneof', '_CheckCalledFromGeneratedFile', '_SetListener', '__class__', '__contains__', '__deepcopy__', '__delattr__', '__delitem__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__getstate__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__iter__', '__le__', '__len__', '__lt__', '__module__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__setitem__', '__setstate__', '__sizeof__', '__slots__', '__str__', '__subclasshook__', '__unicode__', '_extensions_by_name', '_extensions_by_number', 'fields', 'get_or_create_list', 'get_or_create_struct', 'items', 'keys', 'update', 'values']

(Pdb) parameter.ListFields
<built-in method ListFields of Struct object at 0x7f5e0c17e920>

(Pdb) parameter.ListFields()
[(<google.protobuf.pyext._message.FieldDescriptor object at 0x7f5e0c191a10>, {'endDate': string_value: "2019-07-31T23:59:59+02:00"
, 'startDate': string_value: "2019-07-01T00:00:00+02:00"
})]

(Pdb) parameter.endDate
*** AttributeError: endDate

(Pdb) parameter
fields {
  key: "endDate"
  value {
    string_value: "2019-07-31T23:59:59+02:00"
  }
}
fields {
  key: "startDate"
  value {
    string_value: "2019-07-01T00:00:00+02:00"
  }
}

(Pdb) parameter.fields
{'endDate': string_value: "2019-07-31T23:59:59+02:00"
, 'startDate': string_value: "2019-07-01T00:00:00+02:00"
}

(Pdb) type(parameter.fields)
<class 'google.protobuf.pyext._message.MessageMapContainer'>

(Pdb) parameter.fields.get('endDate')
string_value: "2019-07-31T23:59:59+02:00"

我的目标是提取startDateendDate

任何人都可以帮助解释如何系统地处理这种无证的原生动物并从中获取价值吗?

0 个答案:

没有答案