简体   繁体   English

py2neo在循环中添加关系

[英]py2neo adding relationships in a loop

I have the following code which takes a list of domains from my Neo4j database, performs a lookup on the IP and then creates a relationship if one does not already exist. 我有以下代码,该代码从Neo4j数据库中获取域列表,在IP上执行查找,然后创建一个关系(如果尚不存在)。 It works fine up until the last few line of code where the relationships are created. 它可以正常工作,直到创建关系的最后几行代码为止。 I'm getting the following error. 我收到以下错误。 I've confirmed that the lists have two items - the domain and IP so I'm not sure why its creating an error: 我已经确认列表中有两项-域和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

Here is the code: 这是代码:

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']) 

I'm a little confused as to what you are trying to do here. 我对您在这里想做什么感到有些困惑。 For each iteration of your loop for word in list you are resetting whoisnodes to a new list before adding a single item to it on the line below. 对于for word in list循环的每次迭代,您都将whoisnodes重置为新列表,然后在下面的行中向其中添加单个项目。 This means that there can only ever be one item in the list by the time you call get_or_create_relationships , hence the IndexError when you try to access whoisnodes[1] . 这意味着,当您调用get_or_create_relationships ,列表中将永远只有一项 ,因此,当您尝试访问whoisnodes[1]时,将发生IndexError

Did you mean for whoisnodes = [] to be outside the loop? 您是不是whoisnodes = []处于循环之外?

Incidentally, there is also a typo (missing curly bracket): 顺便说一句,还有一个错字(缺少大括号):

whoisindex.get_or_create("whoisID", word, "whoisID":word})

should read: 应该读:

whoisindex.get_or_create("whoisID", word, {"whoisID":word})

my second try although now its returning a JSON error: 我第二次尝试,尽管现在它返回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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM