【问题标题】:Store Json data to JSON file and save them in the CSV file将 Json 数据存储到 JSON 文件并保存在 CSV 文件中
【发布时间】:2022-01-16 09:24:45
【问题描述】:

我试过这种方法,但没有奏效

   with open("data.json", "a", encoding='utf-8') as f:
      json.dump(data,  f,ensure_ascii=False, indent=4 )

但是出现这个问题

#2

我想从 json 转换为 CSV

我想要的一个例子

请告诉我这是否可行

【问题讨论】:

    标签: python json pandas


    【解决方案1】:

    两者都可以通过pandas完成

    【讨论】:

      【解决方案2】:

      由于 JSON 格式的性质,您不能将 JSON 文件一起附加到新的 JSON 中。

      您应该将所有对象收集到一个列表中,然后将该列表写入 JSON 文件,而不是将每个对象单独写入 JSON 文件:

      lst = []
      for data in ...:
          lst.append(data)
      
      with open("data.json", "w", encoding='utf-8') as f:
          #                   ^ notice "a" was changed to "w" here
          json.dump(lst, f, ensure_ascii=False, indent=4)
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2015-10-23
        • 2021-02-11
        • 2015-05-11
        • 1970-01-01
        • 2017-10-17
        • 1970-01-01
        • 1970-01-01
        • 2019-09-28
        相关资源
        最近更新 更多