简体   繁体   中英

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.

      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. You might get the desired outcome if you change the "while" to an "if". You could then take that condition and use it as your while loop condition.

Also, your current code would have the newPayrollId be the same for every iteration in the while loop. I'm assuming the try except should be inside the "for row in table" for loop.

It's also somewhat confusing as you have

firstSsn = ssn

but then in your nested while loop you have

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. You also have an unnecessary True in your while condition. 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. As a side note, it is generally bad practice to nest while loops within other while loops. PEP 20 (The Zen of Python) states that flat is better than nested.

Use break to break the loop ex:

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

This gives the output:

1
2
3
...
98
99

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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