向Neo4j导入数据时遇AttributeError:'NodeMatch'对象无'exists'属性
解决py2neo中
NodeMatch无exists()属性的错误 这个错误是因为py2neo新版本(v4及以上)移除了NodeMatch对象的exists()方法,你可以通过以下方式解决:
推荐方案:用first()判断节点是否存在
first()方法会返回匹配到的第一个节点,若不存在则返回None,以此判断节点是否存在,且效率更高,还能减少重复查询。
修改你的代码逻辑:
# 替换原来的result1相关代码 head = matcher.match("regain", name=str(date_time[0])).first() if not head: head = Node("regain", name=str(date_time[0])) graph.create(head) # 同理处理result2对应的Tail节点 tail = matcher.match("regain", name=str(date_time[1])).first() if not tail: tail = Node("regain", name=str(date_time[1])) graph.create(tail)
修改后的完整代码
import pandas as pd from py2neo import Graph, Node, Relationship, NodeMatcher graph = Graph("http://localhost:7474", username="neo4j", password='123456') matcher = NodeMatcher(graph) path = 'testdata.csv' dates = pd.read_csv(path, names=['Head', 'Tail', 'none', 'fence', 'type'], header=None) print(dates) for ii in dates.index: date_time = dates.loc[ii].values # 处理Head节点 head = matcher.match("regain", name=str(date_time[0])).first() if not head: head = Node("regain", name=str(date_time[0])) graph.create(head) # 处理Tail节点 tail = matcher.match("regain", name=str(date_time[1])).first() if not tail: tail = Node("regain", name=str(date_time[1])) graph.create(tail) # 如需创建关系,可添加以下逻辑(示例) # rel_type = dates.loc[ii]['type'] # rel = Relationship(head, rel_type, tail) # graph.create(rel)
内容的提问来源于stack exchange,提问作者gxyme
相关产品推荐
相关产品推荐

