如何将嵌套回路更改为递归

发布于 2025-01-21 04:15:24 字数 1722 浏览 0 评论 0原文

有人可以帮助我简化代码吗?我想读取XML中的特定内容,但我不知道文件中有多少层。我所能做的就是继续向嵌套代码添加循环。

pn=[]
tooling=[]
up=[]
step=[]
dept=[]
layer=[]
for item in document.iterfind('CustomerPart'):
    for st in item.find('Route').find('Steps'):
        pn.append(item.findtext('CustPartNumber'))
        tooling.append(item.find('PartParameters').findtext('Value27'))
        up.append(item.findtext('UnitPerPanel'))
        step.append(st.findtext('StepNo'))
        dept.append(st.findtext('ResourceName'))
        layer.append(item.findtext('TopLevelBomPartNumber'))
    for sub in item.find('SubAssemblies'):
        for st2 in sub.find('Route').find('Steps'):
            pn.append(item.findtext('CustPartNumber'))
            tooling.append(item.find('PartParameters').findtext('Value27'))
            up.append(item.findtext('UnitPerPanel'))
            step.append(st2.findtext('StepNo'))
            dept.append(st2.findtext('ResourceName'))
            layer.append(sub.find('PartParameters').findtext('Value113'))
        try:
            for sub1 in sub.find('SubAssemblies'):
                for st3 in sub1.find('Route').find('Steps'):
                    pn.append(item.findtext('CustPartNumber'))
                    tooling.append(item.find('PartParameters').findtext('Value27'))
                    up.append(item.findtext('UnitPerPanel'))
                    step.append(st3.findtext('StepNo'))
                    dept.append(st3.findtext('ResourceName'))
                    layer.append(sub1.find('PartParameters').findtext('Value113'))
        except :
            continue
                   
df = pd.DataFrame({'PartNumber': pn, 'tooling':tooling,'Unit Per Panel':up, 'step':step,'dept':dept,'layer':layer})

Can someone help me simplify the code? I want to read specific content inside the XML, but I don't know how many layers there are in the file. All I can do is keep adding loops to the nested code.

pn=[]
tooling=[]
up=[]
step=[]
dept=[]
layer=[]
for item in document.iterfind('CustomerPart'):
    for st in item.find('Route').find('Steps'):
        pn.append(item.findtext('CustPartNumber'))
        tooling.append(item.find('PartParameters').findtext('Value27'))
        up.append(item.findtext('UnitPerPanel'))
        step.append(st.findtext('StepNo'))
        dept.append(st.findtext('ResourceName'))
        layer.append(item.findtext('TopLevelBomPartNumber'))
    for sub in item.find('SubAssemblies'):
        for st2 in sub.find('Route').find('Steps'):
            pn.append(item.findtext('CustPartNumber'))
            tooling.append(item.find('PartParameters').findtext('Value27'))
            up.append(item.findtext('UnitPerPanel'))
            step.append(st2.findtext('StepNo'))
            dept.append(st2.findtext('ResourceName'))
            layer.append(sub.find('PartParameters').findtext('Value113'))
        try:
            for sub1 in sub.find('SubAssemblies'):
                for st3 in sub1.find('Route').find('Steps'):
                    pn.append(item.findtext('CustPartNumber'))
                    tooling.append(item.find('PartParameters').findtext('Value27'))
                    up.append(item.findtext('UnitPerPanel'))
                    step.append(st3.findtext('StepNo'))
                    dept.append(st3.findtext('ResourceName'))
                    layer.append(sub1.find('PartParameters').findtext('Value113'))
        except :
            continue
                   
df = pd.DataFrame({'PartNumber': pn, 'tooling':tooling,'Unit Per Panel':up, 'step':step,'dept':dept,'layer':layer})

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

三生一梦 2025-01-28 04:15:24

为了简化此代码,您实际上不需要递归!您要做的就是删除重复的代码!

添加一个函数以完成所有重复附加,我称此函数append_all(),并且代码变得更加清晰,更简单:

代码:

pn, tooling, up, step, dept, layer=[], [], [], [], [], []

def append_all(item_sub, st, status):
    for st in item.find('Route').find('Steps'):
        pn.append(item.findtext('CustPartNumber'))
        tooling.append(item.find('PartParameters').findtext('Value27'))
        up.append(item.findtext('UnitPerPanel'))
        step.append(st.findtext('StepNo'))
        dept.append(st.findtext('ResourceName'))
        if status == 'st':
            layer.append(item_sub.findtext('TopLevelBomPartNumber'))
        elif status == 'st2' or status == 'st3':
            layer.append(item_sub.find('PartParameters').findtext('Value113'))

for item in document.iterfind('CustomerPart'):
    append_all(item, st, 'st')
    for sub in item.find('SubAssemblies'):
        append_all(sub, st2, 'st2')
        try:
            for sub1 in sub.find('SubAssemblies'):
                append_all(sub1, st3, 'st3')  
        except :
            continue
                   
df = pd.DataFrame({'PartNumber': pn, 'tooling':tooling,'Unit Per Panel':up, 'step':step,'dept':dept,'layer':layer})

To simplify this code, you don't really need a recursion! all you have to do is remove repeated code!

Add a function to do all repeated appends, I called this function append_all() and the code becomes more clear and simple:

code:

pn, tooling, up, step, dept, layer=[], [], [], [], [], []

def append_all(item_sub, st, status):
    for st in item.find('Route').find('Steps'):
        pn.append(item.findtext('CustPartNumber'))
        tooling.append(item.find('PartParameters').findtext('Value27'))
        up.append(item.findtext('UnitPerPanel'))
        step.append(st.findtext('StepNo'))
        dept.append(st.findtext('ResourceName'))
        if status == 'st':
            layer.append(item_sub.findtext('TopLevelBomPartNumber'))
        elif status == 'st2' or status == 'st3':
            layer.append(item_sub.find('PartParameters').findtext('Value113'))

for item in document.iterfind('CustomerPart'):
    append_all(item, st, 'st')
    for sub in item.find('SubAssemblies'):
        append_all(sub, st2, 'st2')
        try:
            for sub1 in sub.find('SubAssemblies'):
                append_all(sub1, st3, 'st3')  
        except :
            continue
                   
df = pd.DataFrame({'PartNumber': pn, 'tooling':tooling,'Unit Per Panel':up, 'step':step,'dept':dept,'layer':layer})
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文