在有效载荷发生变化之前,我的代码运行良好。field_types = [ ('subject', str), ('type', str)]#reading a raw csv fileoutput = []with open('file.csv','r',encoding = 'utf-8-sig') as f: for row in csv.DictReader(f): row.update((key, conversion(row[key])) for key, conversion in field_types) output.append(row) with open('file.json','w') as outfile: #storing records as json json.dump(output,outfile,sort_keys = True, indent = 4)结果保存的很好: { "subject": "1", "type": "2" }, { "subject": "1", "type": "3" }]当前要求它应该保存为 ie 我猜是数组中的 jsonarray。你有过这种情况吗?如何实现? { "subject": { "id": "1" }, "type": "2" }, { "subject": { "id": "1" }, "type": "3" }]
1 回答
四季花海
TA贡献1811条经验 获得超5个赞
这应该做的工作:
def str2dict(s):
return dict(id=s)
field_types = [
('subject', str2dict),
('type', str)
]
添加回答
举报
0/150
提交
取消