简体   繁体   English

dbf文件中没有eof循环时如何做?

[英]How to make do while not eof loop in dbf files?

I have a python program that open a dbf file with about 30000 records and I want to end my while loop but some reason my while loop stays open.我有一个 python 程序,它打开一个包含大约 30000 条记录的 dbf 文件,我想结束我的 while 循环,但由于某种原因我的 while 循环保持打开状态。

      with DBF('data.DBF',recfactory=None,load =True) as table:
                for row in table:
                    stono = (row[1][1])
                    payrollid = (row[15][1])
                    busdate = (row[2][1])
                    firstname =  (row[13][1])
                    lastname = (row[14][1])

                    rows = [[stono,payrollid,busdate]]
                    print(rows) 
                try:
                    while True:
                        boolHasBlank = False
                        lastSsn = ssn
                        firstSsn = ssn 
                        newPayrollId = payrollid.strip()
                        newStoNo = stono
                        Fname = firstname
                        Lname = lastname
                        while True and stono == newStoNo and payrollid == newPayrollId:
                            if ssn is not firstSsn:
                                boolHasBlank = True
                            else:
                                lastSsn = ssn

                except EOFError as e:
                    print (e)

Your nested while loop will never end if it starts because its condition does not change any of the variables used in the condition.如果您的嵌套 while 循环开始,它将永远不会结束,因为它的条件不会更改条件中使用的任何变量。 You might get the desired outcome if you change the "while" to an "if".如果将“while”更改为“if”,您可能会得到想要的结果。 You could then take that condition and use it as your while loop condition.然后,您可以采用该条件并将其用作您的 while 循环条件。

Also, your current code would have the newPayrollId be the same for every iteration in the while loop.此外,您当前的代码将使 while 循环中的每次迭代的 newPayrollId 都相同。 I'm assuming the try except should be inside the "for row in table" for loop.我假设 try except 应该在“for row in table”for循环内。

It's also somewhat confusing as you have这也有点令人困惑,因为你有

firstSsn = ssn

but then in your nested while loop you have但是在你的嵌套while循环中你有

if ssn is not firstSsn:

which would always be False, so then这总是假的,所以

lastSsn = ssn

would be the only thing that ever runs, but you already have lastSsn = ssn before firstSsn = ssn.将是唯一运行过的东西,但你在 firstSsn = ssn 之前已经有了 lastSsn = ssn。 You also have an unnecessary True in your while condition.在你的 while 条件下,你也有一个不必要的 True 。 So, I think your code could be changed to:因此,我认为您的代码可以更改为:

ssn = "something defined earlier"

with DBF('data.DBF', recfactory=None, load=True) as table:

    for row in table:
        stono = (row[1][1])
        payrollid = (row[15][1])
        busdate = (row[2][1])
        firstname = (row[13][1])
        lastname = (row[14][1])

        rows = [[stono, payrollid, busdate]]
        print(rows)

        try:
            while stono == newStoNo and payrollid == newPayrollId:
                boolHasBlank = False
                lastSsn = firstSsn = ssn
                newPayrollId = payrollid.strip()
                newStoNo = stono
                Fname = firstname
                Lname = lastname

        except EOFError as e:
            print(e)

Of course, I could just not have enough information as to what exactly your code does.当然,我可能没有足够的信息来说明您的代码究竟做了什么。 Maybe there's a threaded process that alters the snn value that we can't see.也许有一个线程进程改变了我们看不到的 snn 值。 As a side note, it is generally bad practice to nest while loops within other while loops.附带说明一下,将 while 循环嵌套在其他 while 循环中通常是不好的做法。 PEP 20 (The Zen of Python) states that flat is better than nested. PEP 20 (The Zen of Python) 指出扁平比嵌套更好。

Use break to break the loop ex:使用break打破循环例如:

x = 0
while True:
   print(x)
   x += 1
   if x == 100:
      break

This gives the output:这给出了 output:

1
2
3
...
98
99

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM