【发布时间】:2020-08-01 06:58:47
【问题描述】:
我正在尝试将 json 解析为 csv,分成单独的列,但它不断合并为一个。
人们在下面提出了几种解决方案,但无济于事。我在代码下方发布了我正在使用的数据。
它一直说我需要添加更多细节,但就是这样。只需要一些帮助来弄清楚如何将 json 写入 csv 中的两列...
喂?谁能帮帮我?
replies_final,original_final = [],[]
for i in data['items']:
original = i['snippet']['topLevelComment']['snippet']['textOriginal']
original_final.append(original)
if 'replies' in i:
x = i['replies']['comments'][0]['snippet']['textOriginal']
replies_final.append(x)
with open('test.csv',"a+", newline='',encoding="utf-8") as csv_file:
writer = csv.writer(csv_file)
for item in original_final,replies_final:
writer.writerows([item])
writer.writerows([item])
csv_file.close()
'''
{
"kind": "youtube#commentThread",
"etag": "xJ-LhVfidkBCo_uBKu4E4Cv-YOQ",
"id": "UgxE675r5LTCH_rERuF4AaABAg",
"snippet": {
"videoId": "BEWz4SXfyCQ",
"topLevelComment": {
"kind": "youtube#comment",
"etag": "rA2y-ABxa9twhiI52jjFqt_QNkg",
"id": "UgxE675r5LTCH_rERuF4AaABAg",
"snippet": {
"videoId": "BEWz4SXfyCQ",
"textDisplay": "I don\u2019t know if I believe this guy, my first doubt is why would the us government hire someone from the street to work on a top secret operation. If this was all true, I would imagine they would hand pick the best of the best from within their ranks, cia, nasa, government contractors to work on these secret projects, not just anyone.",
"textOriginal": "I don\u2019t know if I believe this guy, my first doubt is why would the us government hire someone from the street to work on a top secret operation. If this was all true, I would imagine they would hand pick the best of the best from within their ranks, cia, nasa, government contractors to work on these secret projects, not just anyone.",
"authorDisplayName": "Marcos Jimenez",
"authorProfileImageUrl": "https://yt3.ggpht.com/a/AATXAJzupYBdThhYUn47JHZdepkA4W_BtrA3skbUPA=s48-c-k-c0xffffffff-no-rj-mo",
"authorChannelUrl": "http://www.youtube.com/channel/UCrOPNqOqWm9IdO7dyMfHclQ",
"authorChannelId": {
"value": "UCrOPNqOqWm9IdO7dyMfHclQ"
},
"canRate": true,
"viewerRating": "none",
"likeCount": 0,
"publishedAt": "2020-07-31T23:13:07Z",
"updatedAt": "2020-07-31T23:13:07Z"
}
},
"canReply": true,
"totalReplyCount": 0,
"isPublic": true
}
},
{
"kind": "youtube#commentThread",
"etag": "r4rF6BKHllB9T_lR4wsw5pXVNbA",
"id": "UgwuobWtuMcOR8pIh014AaABAg",
"snippet": {
"videoId": "BEWz4SXfyCQ",
"topLevelComment": {
"kind": "youtube#comment",
"etag": "tikGuEG44O99xlCaX05RlAZIPww",
"id": "UgwuobWtuMcOR8pIh014AaABAg",
"snippet": {
"videoId": "BEWz4SXfyCQ",
"textDisplay": "Why is Jeremy Corbell even on this podcast",
"textOriginal": "Why is Jeremy Corbell even on this podcast",
"authorDisplayName": "sg",
"authorProfileImageUrl": "https://yt3.ggpht.com/a/AATXAJyuTS69KYuYl_XBQMdzevIJs48MG6suPK26MQ=s48-c-k-c0xffffffff-no-rj-mo",
"authorChannelUrl": "http://www.youtube.com/channel/UCXcKVpS47zeQ9SBaFBQuOmw",
"authorChannelId": {
"value": "UCXcKVpS47zeQ9SBaFBQuOmw"
},
"canRate": true,
"viewerRating": "none",
"likeCount": 0,
"publishedAt": "2020-07-31T23:05:50Z",
"updatedAt": "2020-07-31T23:05:50Z"
}
},
"canReply": true,
"totalReplyCount": 0,
"isPublic": true
}
},
{
"kind": "youtube#commentThread",
"etag": "8XvNjKtJZwweAoUvikN7bZeVPmU",
"id": "UgyylyZswPsBV55zNYl4AaABAg",
"snippet": {
"videoId": "BEWz4SXfyCQ",
"topLevelComment": {
"kind": "youtube#comment",
"etag": "LIG1LVwaN7Q1604ahnCjvZaRGj8",
"id": "UgyylyZswPsBV55zNYl4AaABAg",
"snippet": {
"videoId": "BEWz4SXfyCQ",
"textDisplay": "The four world class body language experts on the Behavior Panel did an excellent job dissecting and revealing Lazar\u2019s fraud here on YouTube. Now this has become a major embarrassment for both Joe and George Knapp. It has made Joe look extremely foolish and gullible but even worse, it implicates Knapp in the fraud and discredits him as well. So it was not surprising that another podcast was hastily arranged with Knapp and Corbell clutching at straws trying to convince everyone that Lazar is not what the top body language experts say he is \u2013 a bare faced liar who never worked on an alien spaceship.",
"textOriginal": "The four world class body language experts on the Behavior Panel did an excellent job dissecting and revealing Lazar\u2019s fraud here on YouTube. Now this has become a major embarrassment for both Joe and George Knapp. It has made Joe look extremely foolish and gullible but even worse, it implicates Knapp in the fraud and discredits him as well. So it was not surprising that another podcast was hastily arranged with Knapp and Corbell clutching at straws trying to convince everyone that Lazar is not what the top body language experts say he is \u2013 a bare faced liar who never worked on an alien spaceship.",
"authorDisplayName": "It's Just What THEY Told Me, Pinky Swear.",
"authorProfileImageUrl": "https://yt3.ggpht.com/a/AATXAJxMDcotXrLUQ7BQgdwZYlOoC683YKpz-rv9IzDZ=s48-c-k-c0xffffffff-no-rj-mo",
"authorChannelUrl": "http://www.youtube.com/channel/UCMxgt6CqOSwpX8NSlAk3AVg",
"authorChannelId": {
"value": "UCMxgt6CqOSwpX8NSlAk3AVg"
},
"canRate": true,
"viewerRating": "none",
"likeCount": 0,
"publishedAt": "2020-07-31T22:11:30Z",
"updatedAt": "2020-07-31T22:11:30Z"
}
},
"canReply": true,
"totalReplyCount": 2,
"isPublic": true
},
"replies": {
"comments": [
{
"kind": "youtube#comment",
"etag": "_dC_TeoB2nlNsdwqrr4NlUtZrZc",
"id": "UgyylyZswPsBV55zNYl4AaABAg.9Bn9sJEV9_f9BoZFUkY59L",
"snippet": {
"videoId": "BEWz4SXfyCQ",
"textDisplay": "@MiniBull1903 \nHello MiniBull,\r\nI do not have enough interest in the story to be biased but I am very interested in the way humans think and especially in the type of content that triggers an emotional response from them. It will be apparent to others reading this exchange that a lot of what you say in your comment also applies to yourself. I have communicated with you extensively over the last year and you are not so different from Rombert, you both exhibit an above average intelligence and a steely determination. I also think you know that I am not a troll. That is not the main difference between us however, the main difference being something that separates me from all of you and I think that you might be starting to realize what that is now. You wrote: [Who\u2019s right, nobody knows.] Are you sure about that? It would be extremely arrogant to hold such a view without the kind of access that would unequivocally answer the question. There are only TWO possible avenues that would provide the means \u2013 the highest security clearance being but one of them.",
"textOriginal": "@MiniBull1903 \nHello MiniBull,\r\nI do not have enough interest in the story to be biased but I am very interested in the way humans think and especially in the type of content that triggers an emotional response from them. It will be apparent to others reading this exchange that a lot of what you say in your comment also applies to yourself. I have communicated with you extensively over the last year and you are not so different from Rombert, you both exhibit an above average intelligence and a steely determination. I also think you know that I am not a troll. That is not the main difference between us however, the main difference being something that separates me from all of you and I think that you might be starting to realize what that is now. You wrote: [Who\u2019s right, nobody knows.] Are you sure about that? It would be extremely arrogant to hold such a view without the kind of access that would unequivocally answer the question. There are only TWO possible avenues that would provide the means \u2013 the highest security clearance being but one of them.",
"parentId": "UgyylyZswPsBV55zNYl4AaABAg",
"authorDisplayName": "[Visitor by Proxy]",
"authorProfileImageUrl": "https://yt3.ggpht.com/a/AATXAJxR1kpRpG9ePyhUA3YQofnPd5gotjhcbaz6bpiw=s48-c-k-c0xffffffff-no-rj-mo",
"authorChannelUrl": "http://www.youtube.com/channel/UCMmdJDTTFYr1uVNHfTYkCqA",
"authorChannelId": {
"value": "UCMmdJDTTFYr1uVNHfTYkCqA"
},
"canRate": true,
"viewerRating": "none",
"likeCount": 0,
"publishedAt": "2020-08-01T11:12:30Z",
"updatedAt": "2020-08-01T11:12:30Z"
}
},
{
"kind": "youtube#comment",
"etag": "I0sHtZRPYeiPAi1-fQQMk9k0oR0",
"id": "UgyylyZswPsBV55zNYl4AaABAg.9Bn9sJEV9_f9Bo9dYkK3P7",
"snippet": {
"videoId": "BEWz4SXfyCQ",
"textDisplay": "It's Just What THEY Told Me, Pinky Swear. - 12 million views, hardly an embarrassment. You are aware there are other \u2018Body Language experts\u2019 who have no doubt Bob is being truthful? You probably ignore those guys. Bob also passed Polygraph Tests - does this prove he\u2019s truthful, no. Would the trolls be all over it if he had failed said tests - 100% definitely. Who\u2019s right, nobody knows. \nHere\u2019s a fact - if Bob\u2019s story had been proved to be BS beyond doubt, his story would\u2019ve vanished years ago. It evidently triggers the trolls & debunkers that the story is still prevalent today, probably even more so. You do get kudos for your clearly biased & relentless opinion.",
"textOriginal": "It's Just What THEY Told Me, Pinky Swear. - 12 million views, hardly an embarrassment. You are aware there are other \u2018Body Language experts\u2019 who have no doubt Bob is being truthful? You probably ignore those guys. Bob also passed Polygraph Tests - does this prove he\u2019s truthful, no. Would the trolls be all over it if he had failed said tests - 100% definitely. Who\u2019s right, nobody knows. \nHere\u2019s a fact - if Bob\u2019s story had been proved to be BS beyond doubt, his story would\u2019ve vanished years ago. It evidently triggers the trolls & debunkers that the story is still prevalent today, probably even more so. You do get kudos for your clearly biased & relentless opinion.",
"parentId": "UgyylyZswPsBV55zNYl4AaABAg",
"authorDisplayName": "MiniBull1903",
"authorProfileImageUrl": "https://yt3.ggpht.com/a/AATXAJyACocNK2JENn5jN702WVibSX8HAf3YrhmjBJTF=s48-c-k-c0xffffffff-no-rj-mo",
"authorChannelUrl": "http://www.youtube.com/channel/UCZ6mfaV1y3DO1aor1itcIng",
"authorChannelId": {
"value": "UCZ6mfaV1y3DO1aor1itcIng"
},
"canRate": true,
"viewerRating": "none",
"likeCount": 0,
"publishedAt": "2020-08-01T07:28:44Z",
"updatedAt": "2020-08-01T07:39:55Z"
}
}
]
}
【问题讨论】:
-
您的“if 'replies' in i:”仅在 for 循环完全完成且仅检查最后一个“i”时触发。这是故意的吗?
-
@DjerroNeth 为什么会这样?感谢您的信息,正在学习。不,我不打算这样做。
-
因为你的 if 语句在 for 循环之外
-
@DjerroNeth 见上文,复制它是一个错误。现在已更正。还有什么突出的吗?
-
你能快速输出你期望的样例吗?
标签: python json export-to-csv