简体   繁体   中英

Getting a dict key's value as a list (the value is a dict)

I am fairly new to Python so my apologies if the terminology is mistaken. I assume it would be easier to explain through the code itself.

{'continue': {'rvcontinue': '20160625113031|17243371', 'continue': '||'},
 'query': {'pages': {'4270': {'pageid': 4270,
    'ns': 0,
    'title': 'Bulgaristan',
    'revisions': [{'revid': 17077223,
      'parentid': 16909061,
      'user': '85.103.140.217',
      'anon': '',
      'userid': 0,
      'timestamp': '2016-05-11T15:30:31Z',
      'comment': 'BULGARİSTAN',
      'tags': ['visualeditor']},
     {'revid': 17077230,
      'parentid': 17077223,
      'user': 'GurayKant',
      'userid': 406350,
      'timestamp': '2016-05-11T15:31:31Z',
      'comment': '[[Özel:Katkılar/Muratero|Muratero]] ([[Kullanıcı_mesaj:Muratero|mesaj]]) tarafından yapılmış 16907788 numaralı değişiklikler geri getirildi. ([[VP:TW|TW]])',
      'tags': []},
     {'revid': 17079353,
      'parentid': 17077230,
      'user': '85.105.16.34',
      'anon': '',
      'userid': 0,
      'timestamp': '2016-05-12T11:03:43Z',
      'comment': 'Dipnotta 2001 sayımı verilmesine rağmen burada 2011 yazılmış.',
      'tags': ['visualeditor']},
     {'revid': 17085285,
      'parentid': 17079353,
      'user': 'İazak',
      'userid': 200435,
      'timestamp': '2016-05-14T09:36:18Z',
      'comment': 'Gerekçe: Etnik dağılım kaynağı 2001, nüfus sayımı 2011 tarihli.',
      'tags': []},
     {'revid': 17109975,
      'parentid': 17085285,
      'user': 'Kudelski',
      'userid': 167898,
      'timestamp': '2016-05-21T13:14:44Z',
      'comment': 'Düzeltme.',
      'tags': []}]}}}}

From this code I want to get the values of 'pageid' , 'title' , 'revid' , 'user' , 'userid' , 'timestamp' , 'comment' , 'tags' .
I can use y['query']['pages'] however I do not want to continue with '4270' since that number will be changing each time I run the API.

I hope it is explanatory enough! Thank you very much! I can give additional info if necessary!!

You can use list() to convert .keys() or .values() to list and get only first element.

data = {...your dictionary...}

item = list(data['query']['pages'].values())[0]

print('title:', item['title'])
print('pageid:', item['pageid'])

for x in item['revisions']:
    print('---')
    print('revid:', x['revid'])
    print('user:', x['user'])
    print('userid:', x['userid'])
    print('timestamp:', x['timestamp'])
    print('comment:', x['comment'])
    print('tags:', x['tags'])

But if you have many pages in this directory then you should use for -loop

for key, item in data['query']['pages'].items():

    print('key:', key)
    print('title:', item['title'])
    print('pageid:', item['pageid'])

    for x in item['revisions']:
        print('---')
        print('revid:', x['revid'])
        print('user:', x['user'])
        print('userid:', x['userid'])
        print('timestamp:', x['timestamp'])
        print('comment:', x['comment'])
        print('tags:', x['tags'])

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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