如何在字典中循环

问题描述 投票:0回答:1

我在字典中有以下示例字典,如何仅在数据字段中循环并显示键/值。当我运行下面的代码时,它显示出我的关键是数据,没有值

in1 = {
    "data": {
        "id": "1574083",
        "username": "snoopdogg",
        "full_name": "Snoop Dogg",
        "profile_picture": "http://distillery.s3.amazonaws.com/profiles/profile_1574083_75sq_1295469061.jpg",
        "bio": "This is my bio",
        "website": "http://snoopdogg.com",
        "counts": {
            "media": 1320,
            "follows": 420,
            "followed_by": 3410
        }
}}
print(in1['data']['id'])

for k , v in in1.items():
    print("\n RAW DATA:" + k)
    u_info = v['username'] + ' ' + v['full_name']
    print("\tu_info")
python-3.7
1个回答
0
投票

您正在打印引起问题的引号内的u_info

in1 = {
    "data": {
        "id": "1574083",
        "username": "snoopdogg",
        "full_name": "Snoop Dogg",
        "profile_picture": "http://distillery.s3.amazonaws.com/profiles/profile_1574083_75sq_1295469061.jpg",
        "bio": "This is my bio",
        "website": "http://snoopdogg.com",
        "counts": {
            "media": 1320,
            "follows": 420,
            "followed_by": 3410
        }
}}

print(in1['data']['id'])

for k , v in in1.items():
    print("\n RAW DATA:" + k)
    u_info = v['username'] + ' ' + v['full_name']
    print("\t" + u_info)
© www.soinside.com 2019 - 2024. All rights reserved.