将xlsx文件中的三元组导入NEO4J中

# coding: utf-8
from py2neo import Graph,  Node, Relationship
from openpyxl import load_workbook

if __name__ == '__main__':
    # 连接neo4j数据库,输入地址、用户名、密码
    graph = Graph('http://localhost:7474', username='neo4j', password='neo4j')
    workbook = load_workbook(u'./data/xxx.xlsx')
    booksheet = workbook.active
    # 获取sheet页的行数据
    # rows = booksheet.rows
    # 获取sheet页的列数据
    # columns = booksheet.columns
    i = 1
    graph.run('match (n) detach delete n')# 删除所有节点及其关系
	loop = True
    while loop:
        s = []
        i += 1
        # if i > 1000: #取前1000个行
        #     break
        # line = [col.value for col in row]
        entity_1 = booksheet.cell(row=i, column=1).value
        if entity_1 == None:
        	loop = False
            break
        relation_data = booksheet.cell(row=i, column=2).value
        entity_2 = booksheet.cell(row=i, column=3).value
        print(entity_1, relation_data, entity_2)

        entity_node_1 = Node(entity_1, label='patent', name=entity_1)
        s.append(entity_node_1)

        entity_node_2 = Node(entity_2, label='patent', name=entity_2)
        s.append(entity_node_2)

        relationship = Relationship(entity_node_1, relation_data, entity_node_2)
        graph.merge(entity_node_1, entity_1, "name")
        graph.merge(entity_node_2, entity_2, "name")
        graph.create(relationship)



评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值