用的是网上的数据,其中数据的样式为:
先用pandas对文件进行读取
path = r'D:/Invoice_data_Demo.xls' invoice_data = pd.read_excel(path, header = 0) invoice_data.shape # 发现文件是35行27列
先去抽取第一类属性的节点
# 把发票名称抽取出来 invoice_name_list = [] for i in range(0, len(invoice_data)): invoice_name_list.append(invoice_data['发票名称'][i]) invoice_name_list = list(set(invoice_name_list)) #去重
再去抽取第二类属性的节点
# 把除了发票代码这一列去掉,所有的都抽取了,一共抽取了35 * 26 = 910个数据 invoice_value_list = [] for i in range(0, len(invoice_data)): for j in range(1, len(invoice_data.columns)): invoice_value_list.append(invoice_data[invoice_data.columns[j]][i]) invoice_value_list = list(set(invoice_value_list)) # 转变为str类型 invoice_value_list = [str(i) for i in invoice_value_list]
这个数据一共就两类属性的节点,然后我们再去抽取关系
name1_list = [] name2_list = [] rel_list = [] for i in range(0, len(invoice_data)): # 35 for j in range(1, len(invoice_data.columns)): # 1--26 name1_list.append(invoice_data[invoice_data.columns[0]][i]) # 第一类label属性的节点 rel_list.append(invoice_data.columns[j]) # 关系 name2_list.append(invoice_data[invoice_data.columns[j]][i]) name2_list = [str(i) for i in name2_list] # 里面有整数和浮点数,要变成str '''***最关键的一步,把数据变成元组,按照(第一类属性的节点,关系,第二类属性的节点)进行排序''' tuple_total = list(zip(name1_list,rel_list,name2_list))
graph = Graph('你的neo4j的地址', auth = ('账号','密码')) graph.delete_all() # 清除neo4j里面的所有数据 label_1 = '发票名称' label_2 = '发票值' #把节点导入neo4j中 def create_node(invoice_name_list, invoice_value_list): for name in invoice_name_list: node_1 = Node(label_1, name = name) graph.create(node_1) for name in invoice_value_list: node_2 = Node(label_2, name = name) graph.create(node_2) create_node(invoice_name_list, invoice_value_list)
导入以后的示意图如下所示:
第一类属性的节点
第二类属性的节点(只截取一部分):
matcher = NodeMatcher(graph) # 导入关系 for i in range(0, len(tuple_total)): rel = Relationship(matcher.match(label_1, name = tuple_total[i][0]).first(), tuple_total[i][1], matcher.match(label_2, name = tuple_total[i][2]).first() ) graph.create(rel) # 也可以写成下面这种形式 ''' name_1 = matcher.match(label_1, name = tuple_total[i][0]).first() rel = tuple_total[i][1] name_2 = matcher.match(label_2, name = tuple_total[i][2]).first() relationship = Relationship(name_1,rel,name_2) graph.create(relationship) '''
到此,已经把数据全部导入neo4j的数据库中了,效果图为:
代码和数据的链接为:https://github.com/kg5kb8lbj6/simple_kg/tree/main