pythonを使用してExcelデータを読み込み、neo 4 jデータベースの知識スペクトルを作成します.

15492 ワード

最近の研究課題では,知識スペクトルを作成しpythonでコードを書き,主な役割はexcelファイルからノードとノードの関係を読み出しneo 4 jデータベースに作成することであり,最終的な効果はこうである.使用python读取Excel数据,并创建到neo4j数据库知识图谱_第1张图片
# coding:utf-8
import xlwt
import xlrd
from py2neo import Graph, Node, Relationship

##  neo4j   ,    、   、  
graph = Graph('http://localhost:7474', username='neo4j', password='root')

book = xlrd.open_workbook('          .xlsx')
workSheetName = book.sheet_names() # ['    ', '    ', '      ', '    ', '        ']
print("Excel        :"+str(workSheetName))


#         ,                ,    worksheetname
def GetAllSheetCellValue(worksheetname):
    bridgeStructure = book.sheet_by_name(worksheetname)
    AllsheetValue = []
    for i in range(bridgeStructure.nrows):
        for j in range(bridgeStructure.ncols):
            AllsheetValue.append(bridgeStructure.cell_value(i,j))
    # print("《"+str(worksheetname)+"》"+"      ,       ")
    return AllsheetValue

#         ,          
def GetAllSheetValueByColum(worksheetname):
    bridgeStructure = book.sheet_by_name(worksheetname)#         
    col_nums = bridgeStructure.ncols #           
    # print(bridgeStructure)
    AllsheetValue = []
    for i in range(col_nums):
        AllsheetValue.append(bridgeStructure.col_values(i))
    # print(AllsheetValue)
    return AllsheetValue

#          ,  ,    ,               
def CreateNode(className,lableName,name):
    test_node= Node(className,lable = lableName, name=name)
    graph.create(test_node)

#     ,  ,  ,      
def CreateNodes(worksheetname,lableName,ClassName):
    sheetvalue = GetAllSheetCellValue(worksheetname)
    nums = 0
    for i in range(len(sheetvalue)):
        CreateNode(ClassName,lableName,sheetvalue[i])
        nums+=1
    print("  "+worksheetname+"    ,    %s "%(nums))

#              (           ),      ,                   
#      nums:   Excel              
def CreateNodesBySheetNums(nums):
    for i in range(nums):
        CreateNodes(workSheetName[i], workSheetName[i],workSheetName[i])

#           
#     node1:  1,node2:  2,relationship:       
def CreateTwoNodeRelationship(node1,node2,relationship):
    relation = Relationship(node2, relationship, node1)
    graph.create(relation)

#   Excel                ,           (subclassof)  
#      worksheetname:      ,className1:       ,className2:       
def subclassRelationship(worksheetname, className1,className2):
    list1 = GetAllSheetValueByColum(worksheetname)[0]
    list2 = GetAllSheetValueByColum(worksheetname)[2]
    relationship = GetAllSheetCellValue(worksheetname)[1]
    print(list1,relationship,list2)
    #   Python  CQL  
    # graph.run("match(a:    ),(b:    )  where a.name='  ' and b.name='  '  CREATE (a)-[r:DIRECTED]->(b)")

    num=0
    for i in range(len(list1)):
        num+=1
        graph.run("match(a:%s),(b:%s)  where a.name='%s' and b.name='%s'  CREATE (a)-[r:%s]->(b)"%(className1,className2,str(list2[i]),str(list1[i]),str(relationship)))
    print('  %d     '%(num))

CreateNodesBySheetNums(2)
subclassRelationship(workSheetName[2],"    ","    ")
subclassRelationship(workSheetName[3],"    ","    ")
subclassRelationship(workSheetName[4],"    ","    ")

print("      ")


私はどのようにExcelファイルをアップロードするか分かりません.