赞
踩
将csv格式的文件导入到neo4j中,创建节点和关系,代码如下(毕业设计要在其中录入相关题目以及答案,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()
亲测有效,这里创建节点使用的是merge函数,避免了重复创建节点。
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。