mercredi 11 août 2021

How do I include the last instance of the condition met for the if statement?

I have a document that I am reading and writing to. Information is listed with a timestamp at the beginning of every entry. I am trying to populate the dateNTimeArr array with datetime objects for every entry but I notice that the last entry is not appending to the array, no matter how many entries I have. I'm not sure how to solve this.

this an example of the text file I am reading and writing to.

In this example, the array will populate with the datetime objects I create from the first and second entry but it doesn't append the last one.

2021-08-10 16:26:12
123
123
123
123
123
2021-08-10 16:26:28
123
123
123
123
123
2021-08-10 16:27:15
123
123
123
123
123


I tried removing the '\n' from the start of the while loop which seemed to work but the next time I ran the code, it messed with the format and kind of broke. Sorry in advance for the lack of structure to my code.

    f = open("filename", "r")    
    dateNTimeArr = []
    
    for line in f:
        if "2021" in line:
            datentime = line.split(" ")
            datePart = datentime[0]
            timePart = datentime[1]
            hours, mins, secs = timePart.split(":")
            year, month, day = datePart.split("-")
            date1 = date(int(year), int(month), int(day))
            time1 = time(int(hours), int(mins), int(secs))
            datetime1 = datetime.combine(date1, time1)  
            dateNTimeArr.append(datetime1)
            
    f.close()

    f = open("filename", "a+")
    
    
    submitBool = FALSE
    while submitBool == FALSE:
        f.write('\n')
        f.write(now)
        f.write('\n')
        f.write(aQuantity.get())
        f.write('\n')
        f.write(bQuantity.get())
        f.write('\n')
        f.write(cQuantity.get())
        f.write('\n')
        f.write(dQuantity.get())
        f.write('\n')
        f.write(eQuantity.get())
        submitBool = TRUE

    f.close()

Aucun commentaire:

Enregistrer un commentaire