【问题标题】:writing two lists to separate csv columns编写两个列表以分隔 csv 列
【发布时间】: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


【解决方案1】:

试试这个:

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

【讨论】:

    【解决方案2】:

    您需要考虑各种事项,例如,如果您的项目包含多个回复,您可以将它们全部存储到一个临时数组中,如果它不包含任何回复,您可以稍后将其附加到您的 replies_final 对象将保持为空,即使为空,您仍然可以使用与每个项目的 1:1 关系填充您的 replies_final 数组

      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 函数获取两个数组并将它们转换为可以迭代的单个对象,这很好,因为您已经知道对于每个项目,您已经拥有一个 cmets 列表,即使它是空的信息现在将混合。然后你调用了错误的函数而不是使用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(
    ```
    

    【讨论】:

      【解决方案3】:

      我对 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_finalreplies_final 必须具有相同的长度,否则 zip 函数将根据两者中最短的一个来裁剪结果

      所以你可以做的就是像这样修改你的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()
      

      【讨论】:

      • 很遗憾,这不起作用。仍然在一列中。
      • 奇怪....你在 python3 上对吗?并且在每次迭代中item 是一个字符串列表,对吗?它没有嵌套在列表中?
      • 您能否显示这两个列表中的前三个元素original_finalreplies_final?另外,你在做writer.writerows([item]) 而不是writer.writerows(item) 对吗?
      • 更正它是一个字符串列表,是的,我做对了。
      • 还有什么想法吗?我完全被难住了
      猜你喜欢
      • 2022-11-30
      • 2018-10-26
      • 1970-01-01
      • 1970-01-01
      • 2013-12-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-11-23
      相关资源
      最近更新 更多