Python Forum
Double for loop with dates in array
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Double for loop with dates in array
#1
I have a SQLite database that I've selected into an array cursor where each row has a date element. I've already sorted cursor by date.

I effectively have something like:

cursor = [
    [data1, "date", date2, date3],
    [data1, "date", date2, date3],
    [data1, "date", date2, date3],
    [data1, "date", date2, date3]
]
Using two for loops, I want to write a string for each row, group them together by day, and then group and write those days by month per year.

In the end, it should have:

Quote:-> 2020-02.txt

2020-02-01

data
data

2020-02-02

data

2020-02-05

data

-> 2020-03.txt

2020-03-02

data

2020-03-05

data

And so on.

I'm having trouble how to actually structure the for loops to end the "day" and begin another "day", and the same for a "month".

For now, all I have is:
    date_test = date.fromisoformat(cursor[0][2])
    for idx, val in enumerate(sorted(cursor, key=lambda row: row[2])):
        # Check if the year is different
        if date_test.strftime("%Y") != date.fromisoformat(val[2]).strftime("%Y"):
            # Check if month is different...
            pass
Reply
#2
I've written some code that does the work but it is horribly structured:

    cursor.execute("""SELECT 
*
FROM log""")
    records = cursor.fetchall()
    date_index = date.fromisoformat(records[0][2])
    log_file = open("{}.txt".format(date_index.strftime("%Y-%m")), "w")
    log_file.write("""b{}/b:

list
""".format(date_index.strftime("%Y-%m-%d")))
    for idx, val in enumerate(sorted(records, key=lambda row: row[2])):
        date_val = date.fromisoformat(val[2])
        print(date_index, date_val)
        
        # Check if the date is different
        if date_index != date_val:
            print("DEBUG: Date changed")
            
            # Check if day is different so we closed the list.
            if date_index.day != date_val.day:
                log_file.write("""/list

""")
                print("DEBUG: Day changed")
                
            # The month or year is different so we close the file and open an new one
            if date_index.year != date_val.year or date_index.month != date_val.month:
                log_file.close()
                log_file = open("{}.txt".format(date_val.strftime("%Y-%m")), "w")
                log_file.write("""b{}/b:
            
list
""".format(date_val.strftime("%Y-%m-%d")))
                print("DEBUG: month or year changed")
            if date_index.year == date_val.year and date_index.month == date_val.month:
            # The month and year is the same so we open a new list for this day
                log_file.write("""b{}/b:
            
list
""".format(date_val.strftime("%Y-%m-%d")))
            date_index = date_val
        print(val, idx)
        log_file.write(''.join('{}'.format(val)))
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Loop over an an array of array Chendipeter 1 569 Nov-28-2023, 06:37 PM
Last Post: deanhystad
  Compare each element of an array in a logic statement without using a for loop leocsmith 3 5,831 Apr-01-2021, 07:57 PM
Last Post: deanhystad
  Loop different actions for an array Tibovdv 4 2,742 Mar-25-2021, 06:46 PM
Last Post: jefsummers
  Help: list comprehension for loop with double if statement mart79 3 2,409 May-04-2020, 06:34 AM
Last Post: buran
  Loop through array items dynamically in a certain format bhojendra 3 2,635 Jun-11-2019, 03:37 AM
Last Post: micseydel
  change array column values without loop khalidreemy 2 3,744 May-05-2019, 09:05 AM
Last Post: DeaD_EyE
  Convert SAS Dates Format in a loop MohanReddy 2 3,029 Apr-02-2019, 10:31 AM
Last Post: scidam
  N-Dim array manipulation in a loop, getting IndexError: too many indices for array cesardepaula 1 4,436 Mar-13-2019, 01:39 AM
Last Post: scidam
  Putting an array for each string that is printed to a loop ClaudioSimonetti 1 2,332 Feb-05-2019, 12:52 PM
Last Post: perfringo
  loop through list or double loop 3Pinter 4 3,414 Dec-05-2018, 06:17 AM
Last Post: 3Pinter

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020