分析python字典时出现密钥错误
代码是:分析python字典时出现密钥错误,python,json,api,youtube-data-api,Python,Json,Api,Youtube Data Api,代码是: { "kind": "youtube#commentThreadListResponse", "etag": "5b1YCNidguUpH4QsR6mpPJrL6es", "nextPageToken": "QURTSl9pMTQwTEZFU1VRZTB1R2toTFh5djJJSWQzM1oyOXp4Z3ppSXZSNEtNQ25RRzQyRm1xXzFwMDZvc3dqb1
{
"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"
}
}
]
}
},
我为糟糕的格式表示歉意,但我无法将其全部放入代码块中
我正在尝试检索嵌套的回复,然后是注释。我已经在类似的帖子中进行了广泛的搜索,但仍然被卡住了
我不断收到“回复”的关键错误
非常感谢您的帮助。您需要检查密钥是否存在或使用try/except块: 这将为您的给定输入生成:
for i in dct['items']:
try:
print(i['replies']['comments'][0]['snippet']['textOriginal'])
except KeyError:
pass
数据['items']列表的第一个元素中没有任何'replies'键。这就是错误。您在数据['items']列表的第二个元素中有一个'replies'键。您可以检查目录中是否存在答复。对于数据['items']中的i:if'replies'in i:print i['replies']['comments'][0]['snippet']['textOriginal']@ewong right,那么我会使用'[2]'来代替吗?选择第二个元素?
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.