Python "list index out of range" other rule works
Question: I have tried to make a script to read out a csv file and determine some
information.

Now I receive an error:

    
    
    Traceback (most recent call last):
      File "/home/pi/vullijst/vullijst.py", line 26, in <module>
        startdate = datetime.datetime.strptime (row[0],"%d-%m-%Y")
    IndexError: list index out of range
    

Part of Script:

    
    
    import csv
    import datetime
    import smtplib
    from email.mime.multipart import MIMEMultipart
    from email.mime.text import MIMEText
    
    #Variabelen
    smtpserver = ''
    smtplogin = ''
    smtppassword = ''
    sender = ''
    csvfile = '/home/pi/vullijst/vullijst.csv'
    
    #Inlezen CSV File
    f = open(csvfile)
    csv_f = csv.reader(f, delimiter=';')
    today = datetime.datetime.now()
    
    #Tijd bepalen en opstellen E-mail
    for row in csv_f:
    
            startdate = datetime.datetime.strptime (row[0],"%d-%m-%Y")
            enddate = datetime.datetime.strptime (row[1],"%d-%m-%Y")
            if today >= startdate and today <= enddate:
                    receiver = row[3]
    

The csv file has the following structure:

    
    
    1-10-2016;12-10-2016;Test 1;test0@email.com;06-123456789 
    12-10-2016;13-10-2016;Test 2;test1@email.com;06-123456789 
    13-10-2016;14-10-2016;Test 3;test2@email.com;06-123456789 
    14-10-2016;15-10-2016;Test 4;test3@email.com;06-123456790 
    15-10-2016;16-10-2016;Test 5;test4@email.com;06-123456791 
    16-10-2016;17-10-2016;Test 6;test5@email.com;06-123456792 
    17-10-2016;18-10-2016;Test 7;test6@email.com;06-123456793
    

If I comment out this rule then I don't receive the error on the rule below.
Does somebody know what is wrong?


Answer: Your csv file appears to have an empty line at the end, after the last row
with real data. So your script reads all the real lines and processes them,
but it breaks when the last line is parsed into an empty list. The `row[0]`
you're trying to parse into a date isn't valid in that situation.

To avoid this issue, put a check at the top of your loop that skips the rest
of the loop body if `row` is empty.

    
    
    for row in csv_f:
        if not row: # skip empty rows
            continue
        startdate = datetime.datetime.strptime (row[0],"%d-%m-%Y")
        # ...
    

