【发布时间】:2018-03-09 10:35:10
【问题描述】:
我已将 CSV 文件传递给 JSON。我有这样的事情:
[{
"Intent": "About",
"Texto": "quien"
},
{
"Intent": "NP_Contacto",
"Texto": "atencion"
},
{
"Intent": "NP_Contacto",
"Texto": "informacion?"
},
{
"Intent": "NP_Contacto",
"Texto": "hablar"
},
{
"Intent": "NP_Contacto",
"Texto": "numero"
}]
但我想要这个:
[{ "intent": "NP_Contacto",
"texto": [{
"text": "telefono"
},
{
"text": "hablar"
},
{
"text": "número"
},
{
"text": "atención"
},
{
"text": "informacion"
}
]
},
{
"Intent": "About",
"Texto": "quien"
},
]
我用来创建它的代码是:
reader = csv.DictReader(csvfile, fieldnames=("Texto", "Intent"))
fieldnames = ("Texto", "Intent")
output = []
for each in reader:
row = {} for field in fieldnames:
row[field] = each[field]
output.append(row)
json.dump(output, jsonfile, indent=3, sort_keys=True)
【问题讨论】:
-
请向我们展示您已经尝试过的代码。