请帮助我!!我还在学习数据结构和 我正在尝试转换此CSV文件
key,a,b,c,d,e
key_01,apple,100,200,doll,elephant
key_02,apple,100,200,doll,elephant
key_03,apple,100,200,doll,elephant
转换为Json格式。我已经尝试过此脚本
import csv
import json
csvFilePath = 'my.csv'
jsonFilePath = 'my.json'
def make_json(csvFilePath, jsonFilePath):
data = {}
with open(csvFilePath, encoding='utf-8') as csvf:
csvReader = csv.DictReader(csvf)
for rows in csvReader:
key = rows["key"]
data[key] = rows
with open(jsonFilePath, 'w', encoding='utf-8') as jsonf:
jsonf.write(json.dumps(data, indent=4))
make_json(csvFilePath, jsonFilePath)
但是输出中有键行
{
"key_01": {
"key": "01",
"a": "apple",
"y": "100.0",
"c": "cow",
"d": "doll",
"e": "elephant"
},
"key_02": {
"key": "02",
"a": "apple",
"y": "100.0",
"c": "cow",
"d": "doll",
"e": "elephant"
},
}
我希望最终输出看起来像这样。有什么办法可以解决这个问题?请帮助我。
{
"key_01": {
"a": "apple",
"y": "100.0",
"c": "cow",
"d": "doll",
"e": "elephant"
},
"key_02": {
"a": "apple",
"y": "100.0",
"c": "cow",
"d": "doll",
"e": "elephant"
},
"key_03": {
"a": "apple",
"y": "100.0",
"c": "cow",
"d": "doll",
"e": "elephant"
},
答案 0 :(得分:1)
使用dict.pop
删除所需的值:
import csv
import json
from io import StringIO
new_str = """key,a,b,c,d,e
01,apple,100,200,doll,elephant
02,apple,100,200,doll,elephant
03,apple,100,200,doll,elephant
"""
data = {}
csvReader = csv.DictReader(StringIO(new_str))
for rows in csvReader:
key = rows.pop("key")
data[key] = rows
print(json.dumps(data, indent=4))
输出:
{
"01": {
"a": "apple",
"b": "100",
"c": "200",
"d": "doll",
"e": "elephant"
},
"02": {
"a": "apple",
"b": "100",
"c": "200",
"d": "doll",
"e": "elephant"
},
"03": {
"a": "apple",
"b": "100",
"c": "200",
"d": "doll",
"e": "elephant"
}
}
答案 1 :(得分:1)
在代码的这一部分:
for rows in csvReader:
key = rows["key"]
data[key] = rows
del data[key]['key'] # Add this line
答案 2 :(得分:1)
如果您有权访问外部库,则可以使用pandas
库。它使此任务非常简单:
import pandas as pd
# We don't read the key column.
df = pd.read_csv("my.csv", usecols=["a", "b", "c", "d", "e"])
# We create another column here with your desired format.
df["indexer"] = [f"record_{i}" for i in df.index]
# We make it the index, so if you export it to JSON later it will have your desired format.
df.set_index("indexer", inplace=True)
jsn = df.to_json('my.json', orient="index")
# OR, directly to Python dict for further use
dct = df.to_dict(orient="index")