[英]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
完成
To store json data in a.json file, use pandas.DataFrame.to_json
要将 json 数据存储在 .json 文件中,请使用pandas.DataFrame.to_json
To save json data in a.csv file, first use pandas.read_json
to read the data into a dataframe and then use pandas.DataFrame.to_csv
To save json data in a.csv file, first use pandas.read_json
to read the data into a dataframe and then use pandas.DataFrame.to_csv
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.