1.新建csv文件
2.写入文件
3.运行代码
import csv
from py2neo import Graph, Node, Relationship# 连接到Neo4j数据库,使用Bolt协议
graph = Graph("bolt://localhost:7687", auth=("neo4j", "password"))# 读取CSV文件
with open('tanxin.csv', 'r', encoding='utf-8') as csvfile:reader = csv.DictReader(csvfile)nodes_created = {}for row in reader:# 创建节点node_label = row['Type']node_name = row['Name']node_description = row['Description']node_pseudocode = row['Pseudocode']node_time_complexity = row['Time Complexity']node_properties = {'description': node_description,'pseudocode': node_pseudocode,'time_complexity': node_time_complexity}node = Node(node_label, name=node_name, **node_properties)graph.create(node)nodes_created[(node_label, node_name)] = node# 创建关系if row['Relationship'] and row['RelatedNode']:related_node_parts = row['RelatedNode'].split(':')if len(related_node_parts) == 2:related_node_label, related_node_name = related_node_partsif (related_node_label, related_node_name) in nodes_created:related_node = nodes_created[(related_node_label, related_node_name)]relationship = Relationship(node, row['Relationship'], related_node)graph.create(relationship)else:print(f"Related node {related_node_label}:{related_node_name} not found.")else:print(f"Invalid RelatedNode format: {row['RelatedNode']}")# 查询并打印结果
results = graph.run("MATCH (n)-[r]->(m) RETURN n, r, m")
for record in results:print(record)
4.结果展示