分析python字典时出现密钥错误

2024-10-02 18:18:06 发布

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

{
"kind": "youtube#commentThreadListResponse",
"etag": "5b1YCNidguUpH4QsR6mpPJrL6es",
"nextPageToken": "QURTSl9pMTQwTEZFU1VRZTB1R2toTFh5djJJSWQzM1oyOXp4Z3ppSXZSNEtNQ25RRzQyRm1xXzFwMDZvc3dqb1g5dnQyTnVUMVJld2lWVXFta2tFclh2LWk3eENwOFFxMmluTGhlY3JXOHNsSnh4ZlFyNllfdWVWMVlPdkhiWWlnVzA=",
"pageInfo": {
    "totalResults": 100,
    "resultsPerPage": 100
},
"items": [
    {
        "kind": "youtube#commentThread",
        "etag": "GQifP0HFLluusa1n0pFQCxggSvI",
        "id": "UgxWDLFO6d6fhe4UaJd4AaABAg",
        "snippet": {
            "videoId": "BEWz4SXfyCQ",
            "topLevelComment": {
                "kind": "youtube#comment",
                "etag": "YlbdyUbeN1LqFBOqDnQnQZU2DnQ",
                "id": "UgxWDLFO6d6fhe4UaJd4AaABAg",
                "snippet": {
                    "videoId": "BEWz4SXfyCQ",
                    "textDisplay": "Honestly Jeremy is just an annoying piggyback rider",
                    "textOriginal": "Honestly Jeremy is just an annoying piggyback rider",
                    "authorDisplayName": "Michael Myers",
                    "authorProfileImageUrl": "https://yt3.ggpht.com/a/AATXAJwHIfrPXguIZR7YggVntreixLfBisGtlo5xTg=s48-c-k-c0xffffffff-no-rj-mo",
                    "authorChannelUrl": "http://www.youtube.com/channel/UCs4do_iNqxBcxxPmv6U1VPg",
                    "authorChannelId": {
                        "value": "UCs4do_iNqxBcxxPmv6U1VPg"
                    },
                    "canRate": true,
                    "viewerRating": "none",
                    "likeCount": 0,
                    "publishedAt": "2020-07-08T20:55:48Z",
                    "updatedAt": "2020-07-08T20:55:48Z"
                }
            },
            "canReply": true,
            "totalReplyCount": 0,
            "isPublic": true
        }
    },

    {
        "kind": "youtube#commentThread",
        "etag": "wFEgumlYzFR2ZLOsHgEdQoV45SI",
        "id": "UgxaQ38-nL84EgK9ABh4AaABAg",
        "snippet": {
            "videoId": "BEWz4SXfyCQ",
            "topLevelComment": {
                "kind": "youtube#comment",
                "etag": "KyMK87Zq9ej2AHtl44x5-ykwnzQ",
                "id": "UgxaQ38-nL84EgK9ABh4AaABAg",
                "snippet": {
                    "videoId": "BEWz4SXfyCQ",
                    "textDisplay": "Bring bob back and leave captain graybeard at the damn house",
                    "textOriginal": "Bring bob back and leave captain graybeard at the damn house",
                    "authorDisplayName": "Brad Johnson",
                    "authorProfileImageUrl": "https://yt3.ggpht.com/a/AATXAJzzXxTu9bz5hzGL20X1w3ALIcqIWBCc4uzuQPS8=s48-c-k-c0xffffffff-no-rj-mo",
                    "authorChannelUrl": "http://www.youtube.com/channel/UCwTUCnELUJ3IwcBsEwqjNaQ",
                    "authorChannelId": {
                        "value": "UCwTUCnELUJ3IwcBsEwqjNaQ"
                    },
                    "canRate": true,
                    "viewerRating": "none",
                    "likeCount": 1,
                    "publishedAt": "2020-07-08T18:37:35Z",
                    "updatedAt": "2020-07-08T18:37:35Z"
                }
            },
            "canReply": true,
            "totalReplyCount": 1,
            "isPublic": true
        },
        "replies": {
            "comments": [
                {
                    "kind": "youtube#comment",
                    "etag": "eEq9MZRmGGq3sX4IpzEHk_pYvTw",
                    "id": "UgxaQ38-nL84EgK9ABh4AaABAg.9ArZ6N2FniS9ArdOylLUcm",
                    "snippet": {
                        "videoId": "BEWz4SXfyCQ",
                        "textDisplay": "No, because then there'd be no one to distract you from what a fraud Lazar is.",
                        "textOriginal": "No, because then there'd be no one to distract you from what a fraud Lazar is.",
                        "parentId": "UgxaQ38-nL84EgK9ABh4AaABAg",
                        "authorDisplayName": "Rombert Dillahuntsvalle",
                        "authorProfileImageUrl": "https://yt3.ggpht.com/a/AATXAJwALDysFZlmZoXLVeqzSZc6HcvUetsOCk6a2vTY=s48-c-k-c0xffffffff-no-rj-mo",
                        "authorChannelUrl": "http://www.youtube.com/channel/UCpdQrMvl72DIMs1vpsKvpgQ",
                        "authorChannelId": {
                            "value": "UCpdQrMvl72DIMs1vpsKvpgQ"
                        },
                        "canRate": true,
                        "viewerRating": "none",
                        "likeCount": 0,
                        "publishedAt": "2020-07-08T19:23:49Z",
                        "updatedAt": "2020-07-08T19:23:49Z"
                    }
                }
            ]
        }
    },

代码是:

for i in data['items']:
    print (i['replies']['comments'][0]['snippet']['textOriginal'])

我为糟糕的格式表示歉意,但我无法将其全部放入代码块中

我试图检索嵌套的“回复”,然后是“评论”。我已经在类似的帖子中进行了广泛的搜索,但仍然被卡住了

我不断收到“回复”的关键错误

任何帮助都将不胜感激,谢谢


Tags: nocomidtrueyoutubeiscommentsnippet
1条回答
网友
1楼 · 发布于 2024-10-02 18:18:06

您需要检查密钥是否存在,或者使用try/except块:

for i in dct['items']:
    try:
        print(i['replies']['comments'][0]['snippet']['textOriginal'])
    except KeyError:
        pass

这将为您的给定输入生成:

No, because then there'd be no one to distract you from what a fraud Lazar is.

相关问题 更多 >