Python操作Excel(將父子級表頭生成樹狀結構)
阿新 • • 發佈:2019-03-29
import pre pop re.sub != pytho init ret span
import re class Node: ‘‘‘ 容器,用來存儲前後節點信息 ‘‘‘ __slot__=[] def __init__(self,val,next_,pre,name,no): self.val = val self.next = next_ self.pre = pre self.name = name self.no = no class Tree: def __init__(self,li): self.root= Node(-1,[],None,‘‘,-1) cur_node = self.root for no,i,name in li: name = re.sub(r‘[ ]+‘,‘‘,name) while i<cur_node.val: cur_node = cur_node.pre node_ = Node(i,[],cur_node,name,no) if i>cur_node.val: cur_node.next.append(node_) node_.name= cur_node.name+‘_‘+node_.name if cur_node.name!=‘‘ else node_.name elif i == cur_node.val: node_.pre = cur_node.pre cur_node.pre.next.append(node_) node_.name = cur_node.pre.name+‘_‘+node_.name if cur_node.pre.name!=‘‘ else node_.name cur_node= node_ def newNameDict(self): if self.root.next: res = dict() queue = [self.root] while queue: cur = queue.pop(-1) if cur.no==-1: pass else: res[cur.no] = cur.name if cur.next: for i in cur.next: queue.append(i) return res pass
Python操作Excel(將父子級表頭生成樹狀結構)