我的程序似乎没有终止......由于我对 python 比较陌生,我怀疑我犯了一个我还没有看到的常见错误。即使最近在Java中,我也通过关闭文件解决了这样的简单问题......
注意: rt_table
大约有 250,000 行。在这个 python 程序之前,我已经编写了一个等效的 Java 程序,并且执行时间不长。
def create_AMatrix():
"""Create the adjacency table of the retweet network from rt_table to create an adjacency matrix"""
con = mdb.connect(host="localhost", user="root", passwd="", db="twitter")
cur = con.cursor(mdb.cursors.DictCursor)
#get vertex set of users in retweet network
cur.execute("select user_id from users")
rows = cur.fetchall()
vSet = list()
for uID in rows:
vSet.append(uID)
#populate adjacency table
cur.execute("select * from rt_table")
rows = cur.fetchall()
for row in rows:
sourceUserID = row["source_user_id"]
sourceUserName = row["source_user_name"]
rtUserID = row["rt_user_id"]
rtUserName = row["rt_user_name"]
try:
curRow = vSet.index(sourceUserID)
curCol = vSet.index(rtUserID)
except ValueError:
continue
cur.execute("select COUNT(*) from adjacency where r = %s and c = %s", (curRow, curCol))
if cur.fetchone()['COUNT(*)'] == 0:
try:
cur.execute("insert into adjacency (r, c, val, source_user_id, source_user_name, rt_user_id, rt_user_name) values (%d, %d, %d, %d, %s, %d, %s"), (curRow, curCol, 1, sourceUserID, sourceUserName, rtUserID, rtUserName)
con.commit()
except:
con.rollback()
else:
try:
cur.execute("update adjacency set val = val+1 where r = %d and c = %d"), (curRow, curCol)
con.commit()
except:
con.rollback()
cur.close()
con.close()
- 我的错误在哪里?
- 我可以做些什么来找出我的代码在做什么?具体来说,请问程序正在执行哪一行代码?
非常感谢所有帮助,并随时提出建议以使我的代码更加pythonic!