我有存储在数据库中的加权边列表。如何轻松地从中创建图表(无需将其写入文件并读取它)
这里是可复制的
import sqlite3
con = sqlite3.connect(":memory:")
with con:
cur = con.cursor()
cur.execute("CREATE TABLE DATEN(Source TEXT, Target TEXT, Weight REAL)")
cur.execute("INSERT INTO DATEN VALUES('X33', 'X20', 0.014)")
cur.execute("INSERT INTO DATEN VALUES('X32', 'X20', 0.024)")
cur.execute("INSERT INTO DATEN VALUES('X23', 'X20', 0.167)")
cur.execute("INSERT INTO DATEN VALUES('X23', 'X32', 0.015)")
cur.execute("INSERT INTO DATEN VALUES('X32', 'X33', 0.003)")
cur.execute("INSERT INTO DATEN VALUES('X23', 'X33', 0.035)")
cur.execute('SELECT * FROM DATEN')
data = cur.fetchall()
我创建图表的尝试失败:
import networkx as nx
G = nx.Graph()
for x in data:
x1 = {'source': data[0][0], 'target': data[0][1], 'weight': data[0][2]}
print x1
G.add_edge(x1) # THIS IS NOT WORKING
有没有更简单的方法来做到这一点?