Files
GX.O3.WMS/WebApi/docs/analyze_kd_tree.py
T

35 lines
1.2 KiB
Python
Executable File
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import pandas as pd
from collections import Counter
kd = pd.read_excel('物料清单正查_.xlsx', sheet_name='树形单据体')
kd = kd[kd['子项物料编码'].notna()].copy()
kd['code'] = kd['子项物料编码'].astype(str).str.strip()
kd['level'] = kd['BOM层级'].astype(str).str.strip()
kd['name'] = kd['物料名称'].astype(str).str.strip()
# 层级分布
print("=== 金蝶xlsx 层级分布 ===")
for lv, cnt in Counter(kd['level']).most_common():
print(f" 层级 {lv}: {cnt} 条")
# 最大层级
def depth(lv):
return len([x for x in lv.split('.') if x])
kd['depth'] = kd['level'].apply(depth)
print(f"\n最大层级深度: {kd['depth'].max()}")
# 所有层级1.x(第一层子项)
print("\n=== 第一层子项(层级1.x)===")
for _, r in kd[kd['depth']==1].iterrows():
print(f" {r['level']} | {r['code']} | {r['name']}")
# 检查:哪些层级1.x 有下级(1.x.y)
print("\n=== 有下级的中间件(层级1.x 且有 1.x.y 子项)===")
l1 = kd[kd['depth']==1]['level'].tolist()
l2 = kd[kd['depth']>=2]['level'].tolist()
for lv in l1:
has_child = any(x.startswith(lv+'.') for x in l2)
if has_child:
print(f" {lv} 有下级")