You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

向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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.09 12:10:31