简体   繁体   English

将 Json 数据存储到 JSON 文件并保存在 CSV 文件中

[英]Store Json data to JSON file and save them in the CSV file

I tried this way but did not work我试过这种方式但没有用

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

But this problem occurs但是出现这个问题

在此处输入图像描述

#2 #2

I want to convert from json to CSV我想从 json 转换为 CSV

An example of what I want我想要的一个例子

在此处输入图像描述

Please tell me if this is possible请告诉我这是否可能

You can't append JSON files together into a new JSON, because of the nature of the JSON format.您不能将 append JSON 文件一起放入一个新的 JSON,因为 Z0ECD11C1D7A2D87A2F8Z 格式的性质。

Instead of writing each object individually to the JSON file, you should collect all of the objects into a list, and write the list to the JSON file:与其将每个 object 单独写入 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)

Both can be done with pandas两者都可以用pandas完成

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM