[英]py2neo adding relationships in a loop
我有以下代码,该代码从Neo4j数据库中获取域列表,在IP上执行查找,然后创建一个关系(如果尚不存在)。 它可以正常工作,直到创建关系的最后几行代码为止。 我收到以下错误。 我已经确认列表中有两项-域和IP,所以我不确定为什么会产生错误:
File "C:\Python26\beta7_whois4j_monitor_debug.py", line 63, in createrels
rels1 = graph_db.get_or_create_relationships((whoisnodes[0], "links", whoisnodes[1]))
IndexError: list index out of range
这是代码:
whoisindex = graph_db.get_or_create_index(neo4j.Node, "whoisID")
domains = whoisindex.query("whoisID:*com")
for i in domains:
list1 = []
value1 = "{0}".format(i['whoisID'])
try:
e = socket.gethostbyname(value1)
except socket.gaierror:
e = 'exclude from list'
if e != 'exclude from list':
list1.append(value1)
list1.append(e)
for word in list1:
whoisnodes = []
whoisnodes.append(whoisindex.get_or_create("whoisID", word, "whoisID":word}))
rels1 = graph_db.get_or_create_relationships(
(whoisnodes[0], "links", whoisnodes[1]))
print "{0}".format(i['whoisID'])
我对您在这里想做什么感到有些困惑。 对于for word in list
循环的每次迭代,您都将whoisnodes重置为新列表,然后在下面的行中向其中添加单个项目。 这意味着,当您调用get_or_create_relationships
,列表中将永远只有一项 ,因此,当您尝试访问whoisnodes[1]
时,将发生IndexError
。
您是不是whoisnodes = []
处于循环之外?
顺便说一句,还有一个错字(缺少大括号):
whoisindex.get_or_create("whoisID", word, "whoisID":word})
应该读:
whoisindex.get_or_create("whoisID", word, {"whoisID":word})
我第二次尝试,尽管现在它返回JSON错误:
whoisindex = graph_db.get_or_create_index(neo4j.Node, "whoisID")
domains = whoisindex.query("whoisID:*com")
for i in domains:
list1 = []
value1 = "{0}".format(i['whoisID'])
try:
e = socket.gethostbyname(value1)
except socket.gaierror:
e = 'exclude from list'
if e != 'exclude from list':
list1.append(value1)
list1.append(e)
list1.append(whoisindex.get_or_create("whoisID", i, {"whoisID":i}))
rels1 = graph_db.get_or_create_relationships(
(list1[0], "links", list1[1]))
print "{0}".format(i['whoisID'])
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.