我需要带两个tsv文件并将它们组合起来制作一个多级json,我将用它来制作一个图形。
然而,我的边缘列表是限制因素,这意味着我的节点csv中有更多节点,而不是我关心的图形。我只关心如果在我的边缘文件中表示节点。
首先,我需要阅读较小的'边缘列表'tsv。
//Example
source target edgeWeight
NODEA NODE1 27
NODEB NODE2 44
NODEC NODE3 28
NODED NODE4 46
NODEE NODE5 37
NODEF NODE6 46
NODEG NODE7 58
然后我需要检查第二个csv以查看这些节点是否存在。如果是这样,我想将它们添加到elements.node
#check to see
//Example
NodeList edgeWeight
NODEA 7
NODEB 3
NODEC 1
NODEG 9
NODEM 12 // this node wouldn't be created since it isn't present in edges
NODE3 4
NODE4 8
NODE7 1
NODE2 4
NODED 3
NODE1 4
我需要我的最终json文件看起来像这样:
"elements" : {
"nodes" : [ {
"data" : {
"id" : "NODEB",
"weight" : 1
},
}, {
"data" : {
"id" : "NODE3,
"weight" : 20
},
}, {
"data" : {
"id" : "NODED",
"weight" : 60
},
}, {
"data" : {
"id" : "NODE1",
"weight" : 80
},
}, {
"data" : {
"id" : "NODEA",
"weight" : 100
},
} ],
"edges" : [ {
"data" : {
"id" : "1",
"source" : "NODEA",
"target" : "NODED",
"weight": 40
},
}, {
"data" : {
"id" : "2",
"source" : "NODEG",
"target" : "NODE2",
"weight": 4
},
}, {
"data" : {
"id" : "3",
"source" : "NODEA",
"target" : "NODEB",
"weight": 1
},
}, ]
}
}
它适用于JavaScript应用程序,虽然python也可以。
**************************** UPDATE ****************** *********** 这是一些澄清以及我迄今为止所尝试的内容。 分子 / \ 节点边缘 / \ 数据数据 / \ / \ \ ID重量来源目标重量
到目前为止,我已经能够创建一个csv - > pythondict - > json(下面的代码),但仅限于节点和边缘,我不知道如何组合它们。
import csv
import json
import sys
Edgecsvfile = open('SmallEdge.csv', 'r')
#creating new to write
Ejsonfile = open('E2.json', 'wb')
#how to label
Efieldnames = ("source","target","count")
#creating reader
Ereader = csv.DictReader( Edgecsvfile, Efieldnames)
for row in Ereader:
json.dump(row, Ejsonfile, indent = 3)
Ejsonfile.write('\n')
Nodecsvfile = open('SmallNode.csv', 'r')
Njsonfile = open('N2.json', 'wb')
Nfieldnames = ("source","total_mutations")
Nreader = csv.DictReader( Nodecsvfile, Nfieldnames)
for row in Nreader:
json.dump(row, Njsonfile, indent = 2)
Njsonfile.write('\n')
然后我尝试了嵌套字典(下面的代码),但我收到错误“不是JSON可序列化的”
with open ("ETestjsonfile.json", "r") as myfile:
Edgedata=myfile.read().replace('\n', '')
print Edgedata
with open ("NTestjsonfile.json", "r") as myfile:
Nodedata=myfile.read().replace('\n', '')
print Edgedata
Ndata = Nodedata
Edata = Edgedata
target = {'element': {'node': {Ndata}, 'edge': {Edata}}}
print json.dumps(target, indent = 3)
f = open( 'DICTTEST.json', 'w' )
f.write(repr(target) + '\n' )
f.close()