亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

json 數組轉換為 json 數組 - Python

json 數組轉換為 json 數組 - Python

一只名叫tom的貓 2022-12-27 14:48:21
在有效載荷發生變化之前,我的代碼運行良好。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)

]


查看完整回答
反對 回復 2022-12-27
  • 1 回答
  • 0 關注
  • 104 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號