赞
踩
介绍
Py2neo是一个客户端库和工具包,用于从Python应用程序和命令行使用Neo4j(Neo4j Graph Data Platform | Graph Database Management System)。该库同时支持 Bolt 和 HTTP,并提供高级 API、OGM、管理工具、交互式控制台、Pygments 的 Cypher 词法分析器以及许多其他功能。
安装
pip install py2neo
注:Neo4j也需要安装,地址https://neo4j.com/
启动Neo4j
连接代码
- from py2neo import Graph, Node, Relationship
-
- graph = Graph("http://localhost:7474//browser", name='neo4j', auth=("neo4j", "12345678"))
- print(graph.run("show databases"))
demo效果
xls文件内容
sheet 结构
"关系"sheet内容如下
id | from | to | type |
1 | 令狐冲 | 宁中则 | 师娘 |
2 | 仪琳 | 令狐冲 | 倾慕 |
3 | 任盈盈 | 令狐冲 | 爱慕 |
4 | 令狐冲 | 岳林珊 | 小师妹 |
5 | 岳林珊 | 岳不群 | 父亲 |
6 | 任盈盈 | 任我行 | 父亲 |
7 | 岳不群 | 宁中则 | 夫妻 |
#8 | 林平之 | 岳林珊 | 夫妻 |
下面的表格分别对应"恒山派"、"华山派"、"日月教"sheet中的内容()
id | title |
1 | 令狐冲 |
2 | 仪清 |
3 | 仪和 |
4 | 仪琳 |
id | title |
1 | 岳不群 |
2 | 宁中则 |
3 | 岳林珊 |
4 | 林平之 |
id | title |
1 | 任我行 |
2 | 任盈盈 |
3 | 向问天 |
demo依赖
- pip install pandas
- pip install openpyxl
Neo4j和py2neo也是需要的。
demo主要代码块
- import os
-
- import pandas as pd
- from py2neo import Graph, Node, Relationship
-
-
- sheetNameList = [
- {'sheet':'恒山派', 'type':'Node'},
- {'sheet':'华山派', 'type':'Node'},
- {'sheet':'日月教', 'type':'Node'},
- {'sheet':'关系', 'type':'Relationship'},
- ]
- def __init__(self, filename=None):
- self.filename = filename
-
- self.nodeList = []
- self.relationshipList = []
-
- self._loadfile()
- self._buildGraph()
- def _loadfile(self):
- if not os.path.exists(self.filename):
- print('_loadfile', 'file not exist')
- return False
-
- for sheetName in sheetNameList:
- sheetData = pd.read_excel(self.filename, index_col=None, comment='#', sheet_name=sheetName['sheet'])
-
- if sheetName['type'] == 'Node':
- keys=[]
- for index, row in sheetData.iterrows():
- if not row['title']:
- continue
- keys.append(row['title'])
-
- keyval = dict(zip(keys, keys))
- node = Node(sheetName['sheet'], name=sheetName['sheet'], **keyval)
- self.nodeList.append(node)
-
- for index, row in sheetData.iterrows():
- if not row['title']:
- continue
- subNode = Node(sheetName['sheet'], name=row['title'])
- self.nodeList.append(subNode)
- rel = Relationship(node, '属性', subNode)
- self.relationshipList.append(rel)
-
- elif sheetName['type'] == 'Relationship':
- startNode = None
- endNode = None
- for index, row in sheetData.iterrows():
- if not row['from'] or not row['to']:
- continue
-
- for node in self.nodeList:
- if node['name'] == row['from']:
- startNode = node
- elif node['name'] == row['to']:
- endNode = node
- else:
- pass
- rel = Relationship(startNode, row['type'], endNode)
- self.relationshipList.append(rel)
- else:
- pass
- def _buildGraph(self):
- graph = Graph("http://localhost:7474//browser", name='neo4j', auth=("neo4j", "12345678"))
- graph.delete_all()
-
- for node in self.nodeList:
- graph.create(node)
-
- for rel in self.relationshipList:
- graph.merge(rel)
- pass
-
- graph.begin()
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。