如何将嵌套回路更改为递归
有人可以帮助我简化代码吗?我想读取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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
为了简化此代码,您实际上不需要递归!您要做的就是删除重复的代码!
添加一个函数以完成所有重复附加,我称此函数
append_all()
,并且代码变得更加清晰,更简单:代码:
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: