从di中提取数据

2024-04-27 03:44:15 发布

您现在位置:Python中文网/ 问答频道 /正文

这是我正在处理的数据的一个例子。你知道吗

{'etag': '"ld9biNPKjAjgjV7EZ4EKeEGrhao/1v2mrzYSYG6onNLt2qTj13hkQZk"',
 'items': [{'etag': '"ld9biNPKjAjgjV7EZ4EKeEGrhao/Xy1mB4_yLrHy_BmKmPBggty2mZQ"',
            'id': '1',
            'kind': 'youtube#videoCategory',
            'snippet': {'assignable': True,
                        'channelId': 'UCBR8-60-B28hp2BmDPdntcQ',
                        'title': 'Film & Animation'}},
           {'etag': '"ld9biNPKjAjgjV7EZ4EKeEGrhao/UZ1oLIIz2dxIhO45ZTFR3a3NyTA"',
            'id': '2',
            'kind': 'youtube#videoCategory',
            'snippet': {'assignable': True,
                        'channelId': 'UCBR8-60-B28hp2BmDPdntcQ',
                        'title': 'Autos & Vehicles'}},
           {'etag': '"ld9biNPKjAjgjV7EZ4EKeEGrhao/nqRIq97-xe5XRZTxbknKFVe5Lmg"',
            'id': '10',
            'kind': 'youtube#videoCategory',
            'snippet': {'assignable': True,
                        'channelId': 'UCBR8-60-B28hp2BmDPdntcQ',
                        'title': 'Music'}},
 'kind': 'youtube#videoCategoryListResponse'}

我想提取两列数据

  1. '身份证'
  2. '标题'

也就是说,从第一项开始,我将有'id' = 1, 'title' = Film & Animation

我对Python很陌生,用Python做这件事最好的方法是什么?你知道吗

非常感谢大家!你知道吗


Tags: 数据idtruetitleyoutubesnippetetagfilm
3条回答

“items”是一个对象列表,您可以通过for循环对其进行迭代:

for item in your_data['items']:
    id = item['id']
    title = item['snippet']['title']
    print(id, title)

我想是的

for it in data['items']:
  print(it['id'], it['snippet']['title'])

我猜有一个输入错误,'items'键,是一个list,每当我们开始括号([)时,我们需要关闭它们(])。你知道吗

{'etag': '"ld9biNPKjAjgjV7EZ4EKeEGrhao/1v2mrzYSYG6onNLt2qTj13hkQZk"',
 'items': [{'etag': '"ld9biNPKjAjgjV7EZ4EKeEGrhao/Xy1mB4_yLrHy_BmKmPBggty2mZQ"',
            'id': '1',
            'kind': 'youtube#videoCategory',
            'snippet': {'assignable': True,
                        'channelId': 'UCBR8-60-B28hp2BmDPdntcQ',
                        'title': 'Film & Animation'}},
           {'etag': '"ld9biNPKjAjgjV7EZ4EKeEGrhao/UZ1oLIIz2dxIhO45ZTFR3a3NyTA"',
            'id': '2',
            'kind': 'youtube#videoCategory',
            'snippet': {'assignable': True,
                        'channelId': 'UCBR8-60-B28hp2BmDPdntcQ',
                        'title': 'Autos & Vehicles'}},
           {'etag': '"ld9biNPKjAjgjV7EZ4EKeEGrhao/nqRIq97-xe5XRZTxbknKFVe5Lmg"',
            'id': '10',
            'kind': 'youtube#videoCategory',
            'snippet': {'assignable': True,
                        'channelId': 'UCBR8-60-B28hp2BmDPdntcQ',
                        'title': 'Music'}},     # <  -  HERE, no brackets
 'kind': 'youtube#videoCategoryListResponse'}

列表理解

# all items
[row for row in data['items']]   

# custom data from all items
[(row['id'], row['snippet']['title']) for row in data['items']]     

# just id > 0 (filtering data)
[row for row in data['items'] if row['id']>0]   

结合两种过滤数据提取特殊字段:

[(r['id'], r['snippet']['title']) for r in data['items'] if r['id'] > 0]   

也给filter一个机会

真的很酷

f = lambda r: (r['id'], r['snippet']['title'])
result = filter(f, data['items'])

相关问题 更多 >