当前位置:   article > 正文

python将csv文件导入到neo4j数据库_python把csv文件导入neo4j

python把csv文件导入neo4j

将csv格式的文件导入到neo4j中,创建节点和关系,代码如下(毕业设计要在其中录入相关题目以及答案,csv文件内容如下)
csv截图
代码如下:

import csv
import py2neo
from py2neo import Graph, Node, Relationship, NodeMatcher, RelationshipMatcher
import pandas as pd
from pandas import DataFrame
graph = py2neo.Graph('http://localhost:7474', auth=('你的用户名', '你的密码'))
relationshipmatcher = RelationshipMatcher(graph)
matcher = NodeMatcher(graph)
def Importcsv():
    with open('questionbase5.csv', 'r') as f:
        reader = csv.reader(f)
        for item in reader:
            print("当前行数:", reader.line_num, "当前内容:", item)
            if reader.line_num==1:
                continue
            temp=item[6].split('|')#对知识点进行分割处理。
            for i in temp:
                start_node=Node("知识点", name=i)
                end_node=Node("题目",name=item[0],choose1=item[1],choose2=item[2],choose3=item[3],choose4=item[4],answer=item[5])
                graph.merge(start_node, "知识点", "name")
                graph.merge(end_node, "题目","name")
                relation = Relationship(start_node, "相关习题", end_node)
                graph.create(relation)
if __name__ == '__main__':
    Importcsv()

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26

亲测有效,这里创建节点使用的是merge函数,避免了重复创建节点。

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/Monodyee/article/detail/720927
推荐阅读
相关标签
  

闽ICP备14008679号