запись двух списков в отдельные столбцы csv - PullRequest
0 голосов
/ 01 августа 2020

Я пытаюсь разобрать 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"
                    }
                }
            ]
        }

Ответы [ 3 ]

0 голосов
/ 01 августа 2020

Есть разные вещи, которые вам нужно учитывать, например, если ваш элемент содержит более одного ответа, вы можете сохранить их все во временном массиве, который позже можно добавить к своему replies_final объекту, если он не содержит никаких ответов. останется пустым, и вы все равно заполните свой массив replies_final соотношением 1: 1 с каждым элементом, даже если он пуст

  for i in data['items']:
    original = i['snippet']['topLevelComment']['snippet']['textOriginal']
    original_final.append(original)
    item_replies = []
    if 'replies' in i: 
        # Here you can iterate over the replies and add them to a temp array
        for reply in i['replies']:
            x = i['replies']['comments'][0]['snippet']['textOriginal']
            item_replies.append(x)
    replies_final.append(item_replies)

Затем вы можете использовать функцию zip, которая получает оба массива и превращает их в один объект что вы можете перебирать, и это хорошо, потому что вы уже знаете, что для каждого элемента у вас уже есть список комментариев, даже если он пуст, информация теперь будет смешиваться. Тогда вы вызываете неправильную функцию вместо использования writerows(), вам нужно использовать writerow() для вставки по одному

with open('test.csv',"a+", newline='',encoding="utf-8") as csv_file:
    writer = csv.writer(csv_file)
    for i in zip(arr,arr2):
        (item, replies) = i
        writer.writerow([item, replies]))

 csv_file.close(
```
0 голосов
/ 01 августа 2020

Мой эксперимент с тем, как работает этот csv.writer() объект, заключается в том, что

writer.writerow([['a', 'b', 'c'], ['d', 'e', 'f']]) создает файл CSV:

a, b, c
d, e, f

Когда у вас есть for item in original_final, replies_final, тогда на первой итерации items - это список, равный original_final, а на втором итератоне это replies_final, поэтому он создает столбец, содержащий original_final, а затем replies_final

То, что вы хотите получить, находится на первой итерации, item имеет два элемента:

[original_final[0], replies_final[0]]

На второй итерации это должно быть [original_final[1], replies_final[1]]

И так далее

Для этого вы можете используйте функцию zip следующим образом

for item in zip(original_final, replies_final):

Теперь ваша следующая проблема заключается в том, что original_final и replies_final должны быть одинаковой длины, иначе * Функция 1035 * отсекает результат на основе самого короткого из двух

Итак, вы можете изменить свой if оператор следующим образом

if 'replies' in i: 
  x = i['replies']['comments'][0]['snippet']['textOriginal']
  replies_final.append(x)
else:
  replies_final.append('')

Я думаю, это должно сработать

Кроме того, с оператором with вам не нужно делать csv_file.close(). Вы можете просто убрать это

Итак, вот последний блок кода

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)
    else:
       replies_final.append('')


    with open('test.csv',"a+", newline='',encoding="utf-8") as csv_file:
        writer = csv.writer(csv_file)
        for item in zip(original_final,replies_final):
            writer.writerows([item])

Дайте мне знать, если это не сработает

Искусство, ремесла и наука поднимают мир бытия и способствуют его возвышению ~ Бахаулла

РЕДАКТИРОВАТЬ: Поскольку это не работает для вас, что дает вам этот минимальный пример?

import csv

f=open('test.csv', 'a+', newline='', encoding='utf-8')

la=['a', 'b', 'c', 'd']
lb=['A', 'B', 'C', 'D']

writer=csv.writer(f)

for item in zip(la, lb):
    writer.writerows([item])

f.close()
0 голосов
/ 01 августа 2020

Попробуйте это:

with open('test.csv', "a+", newline='', encoding="utf-8") as csv_file:
    writer = csv.writer(csv_file)
    writer.writerows(zip(original_final,replies_final))
...