简体   繁体   English

如何使用“id”打印所有值:

[英]How to print all the values with 'id':

Below is a list of things.. lol sorry quite new at python.. so this is a list?下面是一个列表.. 对不起,python 很新.. 所以这是一个列表?

Anyway, I want to print all the values of the key 'id' ie the first value is 'xxxx@gmail.com'无论如何,我想打印键“id”的所有值,即第一个值是“xxxx@gmail.com”

I have looked for a while and can't seem to find a way to do that.我已经找了一段时间,似乎找不到办法做到这一点。

list_entry = {'kind': 'calendar#calendarList', 'etag': '"p328cl9tbhinuc0g"', 'nextSyncToken': 'CJDKp6uMr-YCEhZwZXRlcmNoaWVuODJAZ21haWwuY29t', 'items': [{'kind': 'calendar#calendarListEntry', 'etag': '"1576118870730000"', 'id': 'xxxx@gmail.com', 'summary': 'peterchien82@gmail.com', 'timeZone': 'Asia/Taipei', 'colorId': '18', 'backgroundColor': '#b99aff', 'foregroundColor': '#000000', 'selected': True, 'accessRole': 'owner', 'defaultReminders': [{'method': 'popup', 'minutes': 2}, {'method': 'popup', 'minutes': 30}], 'notificationSettings': {'notifications': [{'type': 'eventCreation', 'method': 'email'}, {'type': 'eventChange', 'method': 'email'}, {'type': 'eventCancellation', 'method': 'email'}]}, 'primary': True, 'conferenceProperties': {'allowedConferenceSolutionTypes': ['eventHangout']}}, {'kind': 'calendar#calendarListEntry', 'etag': '"1567066298066000"', 'id': 'addressbook#contacts@group.v.calendar.google.com', 'summary': 'Contacts', 'timeZone': 'Asia/Taipei', 'summaryOverride': 'Contacts', 'colorId': '17', 'backgroundColor': '#9a9cff', 'foregroundColor': '#000000', 'selected': True, 'accessRole': 'reader', 'defaultReminders': [], 'conferenceProperties': {'allowedConferenceSolutionTypes': ['eventHangout']}}, {'kind': 'calendar#calendarListEntry', 'etag': '"1567066298066000"', 'id': 'en.canadian#holiday@group.v.calendar.google.com', 'summary': 'Holidays in Canada', 'timeZone': 'Asia/Taipei', 'summaryOverride': 'Holidays in Canada', 'colorId': '7', 'backgroundColor': '#42d692', 'foregroundColor': '#000000', 'selected': True, 'accessRole': 'reader', 'defaultReminders': [], 'conferenceProperties': {'allowedConferenceSolutionTypes': ['eventHangout']}}]}

This looks like a complex dictionary with some lists and dictionaries further embedded in it.这看起来像一个复杂的字典,其中进一步嵌入了一些列表和字典。 To access the information you are looking for, you can use for example the following:要访问您正在寻找的信息,您可以使用例如以下内容:

for i in list_entry['items']: print(i['id'])

This for-loop goes over the list of dictionaries associated with the key 'items' .这个 for 循环遍历与键'items'关联的字典列表。 Each of these dictionaries in turn has a key 'id' , which has associated with it the information you are looking for.这些字典中的每一个都有一个键'id' ,它与您正在寻找的信息相关联。 The output of this code looks like this:此代码的输出如下所示:

xxxx@gmail.com
addressbook#contacts@group.v.calendar.google.com
en.canadian#holiday@group.v.calendar.google.com

You could also save these emails in one variable using list comprehension:您还可以使用列表理解将这些电子邮件保存在一个变量中:

emails = [i['id'] for i in list_entry['items']]

You can use a regex:您可以使用正则表达式:

import re
pat=r"id'[:]\s\'(.*?)\'"

re.findall(pat, str(list_entry))

output:输出:

['xxxx@gmail.com',
 'addressbook#contacts@group.v.calendar.google.com',
 'en.canadian#holiday@group.v.calendar.google.com']

You can use the code below:您可以使用以下代码:

list_entry = {'kind': 'calendar#calendarList', 'etag': '"p328cl9tbhinuc0g"', 'nextSyncToken': 'CJDKp6uMr-YCEhZwZXRlcmNoaWVuODJAZ21haWwuY29t', 'items': [{'kind': 'calendar#calendarListEntry', 'etag': '"1576118870730000"', 'id': 'xxxx@gmail.com', 'summary': 'peterchien82@gmail.com', 'timeZone': 'Asia/Taipei', 'colorId': '18', 'backgroundColor': '#b99aff', 'foregroundColor': '#000000', 'selected': True, 'accessRole': 'owner', 'defaultReminders': [{'method': 'popup', 'minutes': 2}, {'method': 'popup', 'minutes': 30}], 'notificationSettings': {'notifications': [{'type': 'eventCreation', 'method': 'email'}, {'type': 'eventChange', 'method': 'email'}, {'type': 'eventCancellation', 'method': 'email'}]}, 'primary': True, 'conferenceProperties': {'allowedConferenceSolutionTypes': ['eventHangout']}}, {'kind': 'calendar#calendarListEntry', 'etag': '"1567066298066000"', 'id': 'addressbook#contacts@group.v.calendar.google.com', 'summary': 'Contacts', 'timeZone': 'Asia/Taipei', 'summaryOverride': 'Contacts', 'colorId': '17', 'backgroundColor': '#9a9cff', 'foregroundColor': '#000000', 'selected': True, 'accessRole': 'reader', 'defaultReminders': [], 'conferenceProperties': {'allowedConferenceSolutionTypes': ['eventHangout']}}, {'kind': 'calendar#calendarListEntry', 'etag': '"1567066298066000"', 'id': 'en.canadian#holiday@group.v.calendar.google.com', 'summary': 'Holidays in Canada', 'timeZone': 'Asia/Taipei', 'summaryOverride': 'Holidays in Canada', 'colorId': '7', 'backgroundColor': '#42d692', 'foregroundColor': '#000000', 'selected': True, 'accessRole': 'reader', 'defaultReminders': [], 'conferenceProperties': {'allowedConferenceSolutionTypes': ['eventHangout']}}]}

def find_id(d):
    if isinstance(d, dict):
        for k in d:
            if k == 'id':
                print(d[k])
            foo(d[k])
    elif isinstance(d, list):
        for i in d:
            foo(i)

find_id(list_entry)
xxxx@gmail.com
addressbook#contacts@group.v.calendar.google.com
en.canadian#holiday@group.v.calendar.google.com

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

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