如何在复杂的字典中打印一个值

时间:2019-05-14 14:31:37

标签: python

我在词典下面,并且我想在状态中打印“ persistentvolumeclaim“ myclaim”不是“ 如何打印?字典不正确,我删除了很多鳕鱼

{
 'api_version': 'v1',
 'kind': 'Pod',
 'metadata': {
              'annotations': None,
              'uid': 'acdad57c-7632-11e9-90cf-7c8bca006fec'
              },
 'spec': {
          'active_deadline_seconds': None,
          'containers': [
                          {
                            'args': None,     
                            'vsphere_volume': None
                          }
                        ]
         },
 'status': {
             'conditions': [
                             {
                               'last_probe_time': None,
                               'last_transition_time': datetime.datetime(2019, 5, 14, 10, 26, 3, tzinfo=tzutc()),
                               'message': 'persistentvolumeclaim "myclaim" not '
                             }
                           ]                              
             'start_time': None
           }
}

和此代码

    print(dic['status']['conditions'][0]['message']) 

遇到此错误

     print(api_response['status']['conditions'][0]['message'])
        TypeError: 'V1Pod' object is not subscriptable

它看起来像字典,但不是字典。

3 个答案:

答案 0 :(得分:0)

您的JSON不正确。这是正确的JSON和print语句:

class ChargeListener(object):

    ops = []

    @event.listens_for(Charge, 'after_delete', propagate=True)
    def after_delete(mapper, connection, target):
        ChargeListener.add_delete(target)

    @event.listens_for(Charge, 'after_insert', propagate=True)
    def after_insert(mapper, connection, target):
        ChargeListener.add_insert(target)

    @event.listens_for(Charge, 'after_update', propagate=True)
    def after_update(mapper, connection, target):
        ChargeListener.add_insert(target)

    @classmethod
    def execute_ops(cls):
        charges_indexer = ChargesIndexer()
        for op, charge in ChargeListener.ops:
            if op == 'insert':
                charges_indexer.upsert(charge)
            elif op == 'delete':
                charges_indexer.delete(charge)

        ChargeListener.reset()

    @event.listens_for(sess, 'after_flush_postexec')
    def after_flush_postexec(session, flush_context):
        ChargeListener.execute_ops()

    @event.listens_for(sess, 'after_soft_rollback')
    def after_soft_rollback(session, previous_transaction):
        ChargeListener.reset()

    @classmethod
    def add_insert(cls, charge):
        cls.ops.append(('insert', charge))

    @classmethod
    def add_delete(cls, charge):
        cls.ops.append(('delete', charge))

    @classmethod
    def reset(cls):
        cls.ops = []

打印:

a = { 'api_version': 'v1', 'kind': 'Pod', 'metadata': { 'annotations': None, 'uid': 'acdad57c-7632-11e9-90cf-7c8bca006fec' }, 'spec': { 'active_deadline_seconds': None, 'containers': [ { 'args': None, 'vsphere_volume': None } ] }, 'status': { 'conditions': [ { 'last_probe_time': None, 'last_transition_time': datetime.datetime(2019, 5, 14, 10, 26, 3, tzinfo=tzutc()), 'message': 'persistentvolumeclaim "myclaim" not ', 'start_time': None } ] } } print(a['status']['conditions'][0]['message'])

答案 1 :(得分:0)

我终于在代码中找到了条件列表

dic ={'api_version': 'v1',
 'kind': 'Pod',
 'metadata': {'annotations': None,
              'uid': 'acdad57c-7632-11e9-90cf-7c8bca006fec'},
 'spec': {'active_deadline_seconds': None,
          'containers': [{'args': None,

                       'vsphere_volume': None}]},
 'status': {'conditions': [{'last_probe_time': None,
                            'last_transition_time': datetime.datetime(2019, 5, 14, 10, 26, 3, tzinfo=tzutc()),
                            'message': 'persistentvolumeclaim "myclaim" not '}],
            'start_time': None}}

print(dic['status']['conditions'][0]['message']) 

# output persistentvolumeclaim "myclaim" not 

答案 2 :(得分:0)

尝试

  print(a.status.conditions[0].message)

它看起来像字典,但不是。这是一堂课。