user5456868
user5456868

Reputation:

Iterating through CSV file in python with certain conditions

So im trying to iterate through csv file such as the following:

time    date    
25:07   40      
0:07    3       
0:67    1       
0:26    -1       
-1:26   4       

and in the end i have to generate a list with the proper constraints. if its not in the proper constraint then the row wouldnt be generated in the end. the constraints are as this: 1. Illegal time value structure (not HH:MM) and illegal time value (HH < 0 or HH> 23, MM < 0 or MM > 59). 2. Illegal date value (date < 1 or date > 31).

this is what i have tried:

atm_transaction_time_date = []
my_file = open("atm_file_time_date", "r")
reader = (csv.reader(my_file))

header = my_file.readline()

#to check for illegal time
for line in reader:
    if ':' not in (line[0]):
        continue
    elif int(line[0].split(':')[0]) < 0 or int(line[0].split(':')[0]) > 23:
        continue
    else:
        return (line[0].split(':')[0])
    if ':' not in (line[0]):
        continue
    elif int(line[0].split(':')[1]) < 0 or int(line[0].split(':')[1]) > 59:
        continue
    else:
        return (line[0].split(':')[1])

   #to check for illegal date
   if 0 > int(line[1]) > 31:
        continue
    else:
        return int(line[1])

   atm_transaction = (str(line[0]), int(line[1])
   atm_transaction_time_date.append(atm_transaction)

my_file.close()
return atm_transaction_time_date

but it still didnt work. this is the error message Error TypeError : unorderable types: str() < int() raised in function elif (line[0].split(':')[0] < 0) or (line[0].split(':')[0]) > 23:

Upvotes: 1

Views: 440

Answers (1)

blackmamba
blackmamba

Reputation: 1982

The problem is this statement:

int(line[0].split(':')[0] < 0)

You are changing the type of the whole statement, instead of just line[0].split(':')[0]

It should be:

int(line[0].split(':')[0]) < 0 or int(line[0].split(':')[0]) > 23

From the error log, you can see you are comparing a string to an int, which is not allowed. It's just silly mistake that you did, as I can see you wrote it correctly for a similar statement below :)

This is the code:

import csv

atm_transaction_time_date = []
my_file = open("atm_file_time_date", "r")
reader = (csv.reader(my_file))

header = my_file.readline()

#to check for illegal time
for line in reader:
    if ':' not in (line[0]):
        continue
    elif int(line[0].split(':')[0]) < 0 or int(line[0].split(':')[0]) > 23:
        continue
    if ':' not in (line[0]):
        continue
    elif int(line[0].split(':')[1]) < 0 or int(line[0].split(':')[1]) > 59:
        continue

    if 0 > int(line[1]) > 31:
        continue
    atm_transaction = (line[0],int(line[1]))

    atm_transaction_time_date.append(atm_transaction)

print atm_transaction_time_date

return statement doesn't make any sense here. There is no function that you are calling. Continue statement passes to the next iteration of the loop. So, in case if and elif doesn't hold true, it means it's a valid date structure and you can append it to the list.

Upvotes: 1

Related Questions