注册 登录
编程论坛 Python论坛

求助将循环简化为无限循环

yll148 发布于 2023-04-02 13:45, 327 次点击
        sql="Select trim(dwmc) as dwmc,trim(dwcd) as dwcd from 单位名称 where parent is null or trim(parent)='' order by dwcd"
        cur.execute(sql)
        tr0=cur.fetchall()
        for s0 in tr0:
            root= QTreeWidgetItem(self.tree)
            root.setText(0,s0[0])
            root.setText(1,s0[1])
            dwh=s0[1]
            sql="Select trim(dwmc) as dwmc,trim(dwcd) as dwcd from 单位名称 where trim(parent)='"+dwh+"' order by dwcd"
            cur.execute(sql)
            tr1=cur.fetchall()
            if bool(tr1):
                for s1 in tr1:
                    child = QTreeWidgetItem(root)
                    child.setText(0, s1[0])
                    child.setText(1, s1[1])
                    dwh = s1[1]
                    sql="Select trim(dwmc) as dwmc,trim(dwcd) as dwcd from 单位名称 where trim(parent)='"+dwh+"' order by dwcd"
                    cur.execute(sql)
                    tr2=cur.fetchall()
                    if bool(tr2):
                        for s2 in tr2:
                            child2 = QTreeWidgetItem(child)
                            child2.setText(0, s2[0])
                            child2.setText(1, s2[1])
                            dwh= s2[1]
                            sql="Select trim(dwmc) as dwmc,trim(dwcd) as dwcd from 单位名称 where trim(parent)='"+dwh+"' order by dwcd"
                            cur.execute(sql)
                            tr3=cur.fetchall()
                            if bool(tr3):
                                for s3 in tr3:
                                    child3 = QTreeWidgetItem(child2)
                                    child3.setText(0, s3[0])
                                    child3.setText(1, s3[1])
                                    dwh= s3[1]
                                    sql="Select trim(dwmc) as dwmc,trim(dwcd) as dwcd from 单位名称 where trim(parent)='"+dwh+"' order by dwcd"
                                    cur.execute(sql)
                                    tr4=cur.fetchall()
                                    if bool(tr4):
                                        for s4 in tr4:
                                            child4 = QTreeWidgetItem(child3)
                                            child4.setText(0, s4[0])
                                            child4.setText(1, s4[1])
                                            dwh= s4[1]
                                            sql="Select trim(dwmc) as dwmc,trim(dwcd) as dwcd from 单位名称 where trim(parent)='"+dwh+"' order by dwcd"
                                            cur.execute(sql)
                                            tr5=cur.fetchall()
                                            if bool(tr5):
                                                for s5 in tr5:
                                                    child5 = QTreeWidgetItem(child4)
                                                    child5.setText(0, s5[0])
                                                    child5.setText(1, s5[1])
                                                    dwh= s5[1]
                                                    sql="Select trim(dwmc) as dwmc,trim(dwcd) as dwcd from 单位名称 where trim(parent)='"+dwh+"' order by dwcd"
                                                    cur.execute(sql)
                                                    tr6=cur.fetchall()
                                                    if bool(tr6):
                                                        for s6 in tr6:
                                                            child6 = QTreeWidgetItem(child5)
                                                            child6.setText(0, s6[0])
                                                            child6.setText(1, s6[1])
13 回复
#2
东海ECS2023-04-02 16:42
无限循环:

while True:
    循环体

#3
yll1482023-04-02 18:42
回复 2楼 东海ECS
老师能不能具体一点?谢谢!
#4
东海ECS2023-04-02 19:10
也就是说,把你想要死循环的语句块前面加上
while True:

再缩进即可
#5
yll1482023-04-02 19:20
回复 4楼 东海ECS
我还是不太会弄,辛苦老师给列下代码呗?谢谢!谢谢!
#6
东海ECS2023-04-02 20:34
以下是引用yll148在2023-4-2 19:20:31的发言:

我还是不太会弄,辛苦老师给列下代码呗?谢谢!谢谢!

您想要怎么无限循环?
#7
wp2319572023-04-03 07:05
回复 5楼 yll148
说一说你想实现啥功能,无限循环,用户就无法干预了
你不会是想写gui
#8
yll1482023-04-03 07:39
回复 7楼 wp231957
谢谢教师回复,我的想法是TREE层次不受限,有多少个层级就延伸到多少层级,直到都没有子集就结束。
#9
wp2319572023-04-03 09:40
回复 8楼 yll148
这需要递归,不是循环
#10
yll1482023-04-03 09:54
回复 9楼 wp231957
谢谢您的回复,能新辛苦老师帮助解决一下吗?
#11
yll1482023-04-03 15:45
回复 9楼 wp231957
我已经做出来了,谢谢老师!
#12
zxq01032023-04-08 06:47
回复 10楼 yll148
import os
serch_path = 'd:\\python\\'#要查找的目录
ext_name = 'txt'# 要查找的文件后缀名
for r, d, files in os.walk( serch_path ):# 遍历迭代器
    for file in files:# 遍历列表
        if file.endswith( ext_name ): # 如果文件后缀是txt
            print( r+file )#打印出文件路径

[此贴子已经被作者于2023-4-8 07:29编辑过]

#13
yll1482023-04-11 20:08
回复 12楼 zxq0103
谢谢!
#14
一样很高2023-05-07 17:59
回复 10楼 yll148
调用自身
1