英文:
append multiple JSON files to single CSV
问题
在我的Python代码中,我有一个for循环,用于生成扁平化的JSON文件。
我想使用相同的for循环递归地将这些JSON文件追加到一个文件(例如.csv文件)中。
我的最终.csv文件应该如下所示:
{"name": "John", "age": 30, "married": true, "divorced": false, "children": ["Ann", "Billy"], "pets": null, "cars": [{"model": "BMW 230", "mpg": 27.5}, {"model": "Ford Edge", "mpg": 24.1}]}
{"name": "Doe", "age": 33, "married": true, "divorced": false, "children": ["Peter", "Billy"], "pets": null, "cars": [{"model": "Tesla", "mpg": 27.5}, {"model": "Ford Edge", "mpg": 27.1}]}
{"name": "Kurt", "age": 13, "married": true, "divorced": false, "children": ["Bruce", "Nikola"], "pets": null, "cars": [{"model": "Mercedes", "mpg": 27.5}, {"model": "123", "mpg": 24.1}]}
英文:
In my Python code, I have a for loop that generates flattened JSON files.
I would like to append these JSON files recursively to a file (for example a .csv file) using the same for loop.
my final .csv should look like this:
{"name": "John", "age": 30, "married": true, "divorced": false, "children": ["Ann", "Billy"], "pets": null, "cars": [{"model": "BMW 230", "mpg": 27.5}, {"model": "Ford Edge", "mpg": 24.1}]}
{"name": "Doe", "age": 33, "married": true, "divorced": false, "children": ["Peter", "Billy"], "pets": null, "cars": [{"model": "Tesla", "mpg": 27.5}, {"model": "Ford Edge", "mpg": 27.1}]}
{"name": "Kurt", "age": 13, "married": true, "divorced": false, "children": ["Bruce", "Nikola"], "pets": null, "cars": [{"model": "Mercedes", "mpg": 27.5}, {"model": "123", "mpg": 24.1}]}
答案1
得分: 1
解决方法是将所有数据添加到字典或其他数据结构中,如列表。
然后使用csv
库来编写正确的CSV文件。
https://docs.python.org/3/library/csv.html
这将看起来像这样。
我会使用;
,考虑到你有一个用,
分隔的名字列表。
但为什么不将所有内容都写入JSON文件而不是CSV呢?
CSV文件将如下所示。
name;age;married;divorced;children;pets;cars
John;30;true;false;Ann,Billy;;model,BMW 230,mpg,27.5,model,Ford Edge,mpg,24.1
....
最好将所有内容存储在JSON中,这样您可以保留数据架构。
无论如何,您需要将数据存储在字典或列表中,然后使用json
库将其写入JSON文件
https://docs.python.org/3/library/json.html
JSON文件将如您所说的那样。
{"name": "John", "age": 30, "married": true, "divorced": false, "children": ["Ann", "Billy"], "pets": null, "cars": [{"model": "BMW 230", "mpg": 27.5}, {"model": "Ford Edge", "mpg": 24.1}]}
{"name": "Doe", "age": 33, "married": true, "divorced": false, "children": ["Peter", "Billy"], "pets": null, "cars": [{"model": "Tesla", "mpg": 27.5}, {"model": "Ford Edge", "mpg": 27.1}]}
{"name": "Kurt", "age": 13, "married": true, "divorced": false, "children": ["Bruce", "Nikola"], "pets": null, "cars": [{"model": "Mercedes", "mpg": 27.5}, {"model": "123", "mpg": 24.1}]}
英文:
The solution is to add all your data into a dictionary or other data structure like a list.
After that use the csv
library to write a proper CSV file.
https://docs.python.org/3/library/csv.html
Tha will look like this.
I would use ;
considering you have a list of names separated by ,
.
But why not write it all in a JSON file instead of a CSV.
A CSV will look like this.
name;age;married;divorced;children;pets;cars
John;30;true;false;Ann,Billy;;model,BMW 230,mpg,27.5,model,Ford Edge,mpg,24.1
....
It is preferred to store it all in a JSON, so you can keep the data schema.
Anyways you need to store your data in a dict or a list and then write it to a JSON file with json
library
https://docs.python.org/3/library/json.html
The json file will look like you said.
{"name": "John", "age": 30, "married": true, "divorced": false, "children": ["Ann", "Billy"], "pets": null, "cars": [{"model": "BMW 230", "mpg": 27.5}, {"model": "Ford Edge", "mpg": 24.1}]}
{"name": "Doe", "age": 33, "married": true, "divorced": false, "children": ["Peter", "Billy"], "pets": null, "cars": [{"model": "Tesla", "mpg": 27.5}, {"model": "Ford Edge", "mpg": 27.1}]}
{"name": "Kurt", "age": 13, "married": true, "divorced": false, "children": ["Bruce", "Nikola"], "pets": null, "cars": [{"model": "Mercedes", "mpg": 27.5}, {"model": "123", "mpg": 24.1}]}
答案2
得分: 0
你可以安装并导入pandas,然后使用'.to_csv()'。
例如:
import pandas as pd
df = pd.read_json (r'JSON文件路径\文件名.json')
df.to_csv (r'新CSV文件存储路径\新文件名.csv', index = None)
你可以在这里找到文档 -> pandas.pydata.org
英文:
You can install and import pandas, then use a '.to_csv()'.
For ex.:
import pandas as pd
df = pd.read_json (r'Path where the JSON file is saved\File Name.json')
df.to_csv (r'Path where the new CSV file will be stored\New File Name.csv', index = None)
You can find the documentation here -> pandas.pydata.org
答案3
得分: 0
我找到了一个方法来做这件事。我已经在测试,但我做错了一些事情:
csv_filename = csv_tst.csv
with open(input_folder_json_files, 'r') as json_one_line_read:
json_one_line_file = json.load(json_one_line_read)
with open(output_folder + csv_filename, 'a+', newline='') as csv_file:
writer = csv.writer(csv_file)
writer.writerow([json_one_line_file])
这部分代码位于创建扁平化JSON文件的FOR循环内。也许有更优雅的方法来实现它,但它有效。
英文:
I found a way to do it. I was already testing but I was doing something wrong:
csv_filename = csv_tst.csv
with open(input_folder_json_files, 'r') as json_one_line_read:
json_one_line_file = json.load(json_one_line_read)
with open(output_folder + csv_filename, 'a+', newline='') as csv_file:
writer = csv.writer(csv_file)
writer.writerow([json_one_line_file])
This part of the code is within the FOR-loop that creates flattened JSON files. Perhaps there are more elegant ways to do it, but it works.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论