ohr*_*yan 0 python csv arrays json output
我正在尝试从 CSV 中获取数据并将其放入 JSON 格式的顶级数组中。
目前我正在运行此代码:
import csv
import json
csvfile = open('music.csv', 'r')
jsonfile = open('file.json', 'w')
fieldnames = ("ID","Artist","Song", "Artist")
reader = csv.DictReader( csvfile, fieldnames)
for row in reader:
json.dump(row, jsonfile)
jsonfile.write('\n')
Run Code Online (Sandbox Code Playgroud)
CSV 文件的格式如下:
| 1 | Empire of the Sun | We Are The People | Walking on a Dream |
| 2 | M83 | Steve McQueen | Hurry Up We're Dreaming |
Run Code Online (Sandbox Code Playgroud)
其中= 第 1 列:ID | 第 2 栏:艺术家 | 第 3 栏:歌曲 | 第 4 列:专辑
并得到这个输出:
{"Song": "Empire of the Sun", "ID": "1", "Artist": "Walking on a Dream"}
{"Song": "M83", "ID": "2", "Artist": "Hurry Up We're Dreaming"}
Run Code Online (Sandbox Code Playgroud)
我试图让它看起来像这样:
{
"Music": [
{
"id": 1,
"Artist": "Empire of the Sun",
"Name": "We are the People",
"Album": "Walking on a Dream"
},
{
"id": 2,
"Artist": "M83",
"Name": "Steve McQueen",
"Album": "Hurry Up We're Dreaming"
},
]
}
Run Code Online (Sandbox Code Playgroud)
Pandas 非常简单地解决了这个问题。首先读取文件
import pandas
df = pandas.read_csv('music.csv', names=("id","Artist","Song", "Album"))
Run Code Online (Sandbox Code Playgroud)
现在你有一些选择。从中获取正确的 json 文件的最快方法很简单
df.to_json('file.json', orient='records')
Run Code Online (Sandbox Code Playgroud)
输出:
[{"id":1,"Artist":"Empire of the Sun","Song":"We Are The People","Album":"Walking on a Dream"},{"id":2,"Artist":"M83","Song":"Steve McQueen","Album":"Hurry Up We're Dreaming"}]
Run Code Online (Sandbox Code Playgroud)
这不能满足您希望所有内容都在“音乐”对象或字段顺序中的要求,但它确实具有简洁性的好处。
要将输出包装在 Music 对象中,我们可以使用to_dict:
import json
with open('file.json', 'w') as f:
json.dump({'Music': df.to_dict(orient='records')}, f, indent=4)
Run Code Online (Sandbox Code Playgroud)
输出:
{
"Music": [
{
"id": 1,
"Album": "Walking on a Dream",
"Artist": "Empire of the Sun",
"Song": "We Are The People"
},
{
"id": 2,
"Album": "Hurry Up We're Dreaming",
"Artist": "M83",
"Song": "Steve McQueen"
}
]
}
Run Code Online (Sandbox Code Playgroud)
我建议您重新考虑坚持字段的特定顺序,因为JSON 规范明确指出“对象是一组无序的名称/值对”(强调我的)。