在有效載荷發生變化之前,我的代碼運行良好。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
提交
取消