第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問(wèn)題,去搜搜看,總會(huì)有你想問(wèn)的

TypeError: {'name:busi,group:1'} 在 python 中從頭開(kāi)始構(gòu)建

TypeError: {'name:busi,group:1'} 在 python 中從頭開(kāi)始構(gòu)建

慕無(wú)忌1623718 2021-10-26 16:10:47
我們?nèi)绾瓮ㄟ^(guò)使用csv數(shù)據(jù)或作為列表提供的數(shù)據(jù)在python中動(dòng)態(tài)創(chuàng)建json文件data.csv 看起來(lái)像這樣busi,bizthank,pumpthank,suchthank,merri它顯示了圖中節(jié)點(diǎn)之間的鏈接/邊。我正在嘗試構(gòu)建節(jié)點(diǎn)和數(shù)組的 json,但出現(xiàn) json 序列化錯(cuò)誤。nodes = []nodes_dict = {}counter = 0edges = open(edges_file)    for line in edges:        line = line.replace("\n","")        source =  line.split(",")[0]        target = line.split(",")[1]        if source not in nodes_dict:            node_arr = {"name:" + source  + "," + "group:1"}            nodes.append(node_arr)            nodes_dict[source] = counter            counter += 1        if target not in nodes_dict:            node_arr = {"name:"+ target + "," + "group:1"}            nodes.append(node_arr)            nodes_dict[target] = counter            counter += 1 json.dumps(nodes,outputfile)但我收到此錯(cuò)誤類(lèi)型錯(cuò)誤:{'name:busi,group:1'} 不是 JSON 可序列化的我想要的輸出 "nodes":[    {"name":"Myriel","group":1},        {"name":"Napoleon","group":1},{"name":"Mme.Hucheloup","group":8}],"links":            [{"source":1,"target":0,"value":1},{"source":2,"target":0,"value":8},                {"source":3,"target":0,"value":10},{"source":3,"target":2,"value":6}}}
查看完整描述

1 回答

?
開(kāi)滿(mǎn)天機(jī)

TA貢獻(xiàn)1786條經(jīng)驗(yàn) 獲得超13個(gè)贊

你正在構(gòu)建你的 dict 錯(cuò)誤。


嘗試這樣的事情


nodes = []

nodes_dict = {}

counter = 0

edges = open(edges_file)

    for line in edges:

        line = line.replace("\n","")

        source =  line.split(",")[0]

        target = line.split(",")[1]

        if source not in nodes_dict:

            node_arr = dict(

                name=source,

                group=1

            )

            nodes.append(node_arr)

            nodes_dict[source] = counter

            counter += 1


        if target not in nodes_dict:

            node_arr = dict(

                name=target,

                group=1

            )

            nodes.append(node_arr)

            nodes_dict[target] = counter

            counter += 1

 json.dumps(nodes,outputfile)


查看完整回答
反對(duì) 回復(fù) 2021-10-26
  • 1 回答
  • 0 關(guān)注
  • 232 瀏覽
慕課專(zhuān)欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢(xún)優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)