使用networkx包可视化数据时python中的keyError

使用networkx包可视化数据时python中的keyError,python,twitter,networkx,Python,Twitter,Networkx,我正在尝试下载twitter帐户和追随者信息,试图通过使用Networkx python包和Gaphi创建关系图来可视化数据 import networkx as nx import MySQLdb conn = MySQLdb.connect(host="localhost", # your host, usually localhost user="root", # your username p

我正在尝试下载twitter帐户和追随者信息,试图通过使用Networkx python包和Gaphi创建关系图来可视化数据

    import networkx as nx
    import MySQLdb

    conn = MySQLdb.connect(host="localhost", # your host, usually localhost
                 user="root", # your username
                  passwd="123456", # your password
                  db="twitterbank") # name of the data base
    cur = conn.cursor()

    def get_user_info(m):
        cur.execute("SELECT tweeter_name FROM tweets_fetch where tweeter_id=%s" %m)

    g=nx.Graph()

    def add_node_tw(n,weight=None,time=None,location=None):
        if not g.has_node(n):
            screen_name=get_user_info(n)
            g.add_node(n)
            g.node[n]['weight']=1
            g.node[n]["screen_name"]=screen_name
        else:
            g.node[n]['weight']+=1

    def add_edge_tw(n1,n2,weight=None):
        if not g.has_edge(n1,n2):
            g.add_edge(n1,n2)
            g[n1][n2]['weight']=1
        else:
            g[n1][n2]['weight']+=1

    #generate set of users

     users=set()
     cur.execute("SELECT distinct tweeter_id FROM tweets_fetch")
     cur.fetchall() 
     for row in cur:  
          users.add(row[0])


    g=nx.DiGraph()

    for u_id in users:
        add_node_tw(u_id)
        cur.execute("select * from tweeter_followers where tweeter_id=%s" %u_id)
        cur.fetchall()
        for row1 in cur:
            if row1[0] in users:
                add_node_tw(row1[0])
                add_edge_tw(row1[0],row1[1])
    nx.write_graphml(g,'relationship_graphml')  
我用下载的数据创建的两个表是:
tweets\u fetch:带有列(tweeter\u id、tweeter\u name、tweet\u内容、日期时间…

tweeter\u followers:带有列(tweeter\u id,follower\u id)

执行上述代码时,会弹出如下错误:

    Traceback (most recent call last):
    File "D:\Sepups\eclipse-SDK-3.7.1-win32-    x86_64\eclipse\plugins\org.python.pydev_2.7.3.2013031601\pysrc\pydevd.py", line 1397, in <module>
    debugger.run(setup['file'], None, None)
    File "D:\Sepups\eclipse-SDK-3.7.1-win32-x86_64\eclipse\plugins\org.python.pydev_2.7.3.2013031601\pysrc\pydevd.py", line 1090, in run
    pydev_imports.execfile(file, globals, locals) #execute the script
    File "D:\java\python\workspace\tweetsHarvest\src\tweet_graph.py", line 47, in <module>
    add_node_tw(u_id)
    File "D:\java\python\workspace\tweetsHarvest\src\tweet_graph.py", line 24, in add_node_tw
    g.node[n]['weight']+=1
    KeyError: 'weight'
回溯(最近一次呼叫最后一次):
文件“D:\Sepups\eclipse-SDK-3.7.1-win32-x86\u 64\eclipse\plugins\org.python.pydev\u 2.7.3.2013031601\pysrc\pydevd.py”,第1397行,在
运行(安装程序['file'],无,无)
文件“D:\Sepups\eclipse-SDK-3.7.1-win32-x86\u 64\eclipse\plugins\org.python.pydev\u 2.7.3.2013031601\pysrc\pydevd.py”,第1090行,正在运行
pydev_imports.execfile(文件、全局、局部)#执行脚本
文件“D:\java\python\workspace\tweetsHarvest\src\tweet\u graph.py”,第47行,在
添加节点(u\u id)
文件“D:\java\python\workspace\tweetsHarvest\src\tweet\u graph.py”,第24行,在add\u node\u tw中
g、 节点[n]['weight']+=1
关键错误:“重量”

有人知道怎么修吗?我是python和Gephi的新手。我在创建代码时引用的博客是

我基于相同的代码创建了一个脚本,并且在使用一个数据集时出现了相同的错误。如果您和我有相同的问题,那么数据中的一些行就有问题。对我来说,这只是几千条边中的一把。要诊断问题所在,可以在add_edge_tw语句之前打印每一行,并在add_edge_tw之前添加try/except子句

我相信其他擅长Python和NetworkX的人可以给出更好的答案,但希望这有助于您在诊断时快速修复