通过Python读取Excel表内容导入Neo4j数据库中
1 #!/usr/bin/python 2 # -*- coding:utf-8 -*- 3 from py2neo import Node, Relationship, Graph 4 import xlrd 5 6 7 #读取Excel表 路径 表名 8 def readExcelDataByName(fileName, sheetName): 9 data = xlrd.open_workbook(fileName) 10 table = data.sheet_by_name(sheetName) 11 return table 12 13 #获取Excel列表名 14 def getColumnIndex(table, columnName): 15 columnIndex = None 16 for i in range(table.ncols): 17 if(table.cell_value(0, i) == columnName): 18 columnIndex = i 19 break 20 return columnIndex 21 22 #获取Excel行值 23 def getNrowValue(table, nrow, columnName): 24 nrowsValue = table.cell_value(nrow, getColumnIndex(table, columnName)) 25 return nrowsValue 26 27 #创建节点 类别,名字,特征,位置,图片,视频,3D模型,id 28 def CreateNode(m_class, m_name, m_feature, m_location, m_img, m_video, m_model): 29 m_node = Node(m_class, name=m_name, feature=m_feature, location=m_location, img=m_img, video=m_video, model=m_model) 30 graph.create(m_node) 31 32 #创建关系 node_a-[m_relation]->node_b 33 def CreateRelationship(node_a, node_b, m_relation): 34 relation = Relationship(node_a, m_relation, node_b) 35 graph.create(relation) 36 37 #查询节点 类别,名字,特征,位置,图片,视频,3D模型,id 38 def MatchNode(m_class, m_name, m_feature, m_location, m_img, m_video, m_model): 39 node = graph.nodes.match(m_class, name=m_name, feature=m_feature, location=m_location, img=m_img, video=m_video, model=m_model).first() 40 return node 41 42 43 if __name__ == "__main__": 44 graph = Graph('http://localhost:7474', username='neo4j', password='123456') #连接数据库 45 excelfile = r'E:\work\neo4j_python\' 46 sheetname = 'Sheet1' 47 table = readExcelDataByName(excelfile, sheetname) 48 #Excel行数 49 nrows = table.nrows 50 #创建节点 51 for i in range(1, nrows): 52 m_class = getNrowValue(table, i, 'Class') 53 m_name = getNrowValue(table, i, 'Name') 54 m_feature = getNrowValue(table, i, 'Feature') 55 m_location = getNrowValue(table, i, 'Location') 56 m_img = getNrowValue(table, i, 'Image') 57 m_video = getNrowValue(table, i, 'Video') 58 m_model = getNrowValue(table, i, 'Model') 59 node = MatchNode(m_class, m_name, m_feature, m_location, m_img, m_video, m_model) 60 #查询node是否存在 61 if node == None: 62 CreateNode(m_class, m_name, m_feature, m_location, m_img, m_video, m_model) 63 #创建关系 64 for i in range(1, nrows): 65 66 #节点a 67 m_relationship = getNrowValue(table, i, 'Relationship') #关系 a-[r]->b 68 m_name_a = getNrowValue(table, i, 'Name') 69 m_class_a = getNrowValue(table, i, 'Class') 70 m_feature_a = getNrowValue(table, i, 'Feature') 71 m_location_a = getNrowValue(table, i, 'Location') 72 m_img_a = getNrowValue(table, i, 'Image') 73 m_video_a = getNrowValue(table, i, 'Video') 74 m_model_a = getNrowValue(table, i, 'Model') 75 node_a = MatchNode(m_class_a, m_name_a, m_feature_a, m_location_a, m_img_a, m_video_a, m_model_a) 76 #node_a = graph.nodes.match(m_class_a, name=m_name_a).first() 77 print(node_a) 78 79 #节点b 80 id_b = getNrowValue(table, i, 'IDB') 81 #判断IDB是否为空 82 if len(id_b) != 0: 83 id_b = id_b.split(", ") #以", " 分割 逗号+空格 84 id_b = list(map(int, id_b)) 85 86 for i in id_b: 87 m_name_b = getNrowValue(table, i, 'Name') 88 m_class_b = getNrowValue(table, i, 'Class') 89 m_feature_b = getNrowValue(table, i, 'Feature') 90 m_location_b = getNrowValue(table, i, 'Location') 91 m_img_b = getNrowValue(table, i, 'Image') 92 m_video_b = getNrowValue(table, i, 'Video') 93 m_model_b = getNrowValue(table, i, 'Model') 94 node_b = MatchNode(m_class_b, m_name_b, m_feature_b, m_location_b, m_img_b, m_video_b, m_model_b) 95 #node_b = graph.nodes.match(m_class_b, name=m_name_b).first() 96 print(node_b) 97 CreateRelationship(node_a, node_b, m_relationship)
使用了xlrd、py2neo操控Excel的库和连接操作Neo4j的库
Excel表中关系填写如下:
IDA(数据序号) 类别 名称 特征 位置 图片 视频 模型 IDB(包含关系序号) 关系名称
IDB(数字通过填写英文逗号+空格来隔开 程序通过此分割每个数字)
IDB填写完后最好文本化(分列-->分隔符号-->下一步-->文本)

浙公网安备 33010602011771号