太原做网站公司运营,简单的app开发,vue 做门户网站,营销方案100例1.导入txt文件
数据示例#xff1a;
#xff08;头实体#xff0c;关系#xff0c;尾实体#xff09;
#xff08;头实体#xff0c;关系#xff0c;尾实体#xff09;
。。。。。。在执行python代码之前在neo4j中执行这个命令#xff0c;清空所有节点 match (n) d…1.导入txt文件
数据示例
头实体关系尾实体
头实体关系尾实体
。。。。。。在执行python代码之前在neo4j中执行这个命令清空所有节点 match (n) detach delete n
代码
import py2neo
from py2neo import Graph,Node,Relationship,NodeMatcher
import csvgraph Graph(http://localhost:7474,userneo4j,password自己的密码neo4j的初始密码是neo4j)# 打开txt文件
with open(数据文件路径, r, encodingutf-8) as file:lines file.readlines()for line in lines:# 分割每行数据triple line.strip().split(, )triple[0] triple[0].replace((, )triple[2] triple[2].replace(), )# 创建节点和关系head_node Node(Entity, nametriple[0], primary_labelEntity, primary_keyname)tail_node Node(Entity, nametriple[2], primary_labelEntity, primary_keyname)relationship Relationship(head_node, triple[1], tail_node)graph.merge(head_node, Entity, name)graph.merge(tail_node, Entity, name)graph.merge(relationship)#关闭连接 这行代码会报错AttributeError: Graph object has no attribute close
# 是为了看什么时候导入完成报错的时候说明前面的执行完了就导入完成了
graph.close()
2.导入csv格式
数据示例
头实体关系尾实体头实体关系尾实体
代码
with open(数据文件路径, r, encodingutf-8) as f:reader csv.reader(f)for item in reader:head_node Node(Entity, nameitem[0], primary_labelEntity, primary_keyname)tail_node Node(Entity, nameitem[2], primary_labelEntity, primary_keyname)relationship Relationship(head_node, item[1], tail_node)graph.merge(head_node, Entity, name)graph.merge(tail_node, Entity, name)graph.merge(relationship)#关闭连接
graph.close()路虽远行则将至。