python将csv文件导入neo4j数据库中


其实就是按行解析csv文件,并将其中数据分为“结点”、“关系”两种类型,构建利用Node()方法构建节点;Relationship()方法构建关系

csv结构

entity1,relation1,entity2,relation2,info,relation3,keyword
人格纠纷权,包含,一般人格权纠纷,描述,一般人格权纠纷是指因侵害他人的一般人格权,使他人的人格利益受损而引起的纠纷。,关键词,人格权、纠纷、指因、人格、利益

 

from py2neo import Graph, Node, Relationship
import pandas as pd

df = pd.read_csv('crime.csv', error_bad_lines=False, encoding='utf-8')
df = df.fillna('unknown') #填充缺失值

new = df['entity1'].str.strip()
df['entity1'] = new
new = df['entity2'].str.strip()
df['entity2'] = new
new = df['info'].str.strip()
df['info'] = new
new = df['keyword'].str.strip()
df['keyword'] = new

new = df['relation1'].str.strip()
df['relation1'] = new
new = df['relation2'].str.strip()
df['relation2'] = new
new = df['relation3'].str.strip()
df['relation3'] = new


# 连接neo4j数据库,输入地址、用户名、密码
graph = Graph("http://localhost:7474", username="neo4j", password='password')
graph.delete_all()
graph.begin()

# 创建结点
for i in range(len(df['entity1'])):
    #设置node
    node1 = Node('entity1', name=df['entity1'][i])
    graph.merge(node1, 'entity1', 'name')
    node2 = Node('entity2', name=df['entity2'][i])
    graph.merge(node2, 'entity2', 'name')
    node3 = Node('info', name=df['info'][i])
    graph.merge(node3, 'info', 'name')
    node4 = Node('keyword', name=df['keyword'][i])
    graph.merge(node4, 'keyword', 'name')

    #设置relation
    rel1 = Relationship(node1, df['relation1'][i], node2)
    rel2 = Relationship(node2, df['relation2'][i], node3)
    rel3 = Relationship(node3, df['relation3'][i], node4)
    graph.merge(rel1)
    graph.merge(rel2)
    graph.merge(rel3)

print("success")

 

原创文章,作者:端木书台,如若转载,请注明出处:https://blog.ytso.com/267345.html

(0)
上一篇 2022年6月15日
下一篇 2022年6月15日

相关推荐

发表回复

登录后才能评论