今天写一个老师的任务 在neo4j中导入json格式的知识图谱 但总是失败在第一步连接
graph = Graph(“bolt://localhost:7687”,username=“neo4j”,password = “1234”)上 贴上代码
import json
from py2neo import Node, Relationship, NodeMatcher, Graph
graph = Graph(“bolt://localhost:7687”,username=“neo4j”,password = “1234”)
with open(‘微观经济学.json’, ‘r’, encoding=‘utf-8’) as file:
str = file.read()
results = json.loads(str)
matcher = NodeMatcher(graph)
for item in results:
a = matcher.match(‘object’, name=item.get(‘object’)).first()
if not a:
a = Node(‘object’, name = item.get(‘object’))
b = matcher.match(‘object’, name=item.get(‘subject’)).first()
if not b:
b = Node(‘object’, name = item.get(‘subject’))
r = Relationship(b, item.get('relation') ,a)
graph.create(a)
grap