pythoncsv

how to make a function that check if the csv file is valid or not? (python)


i have a csv file like so name: test1.csv

read_start,read_end
22,90
15,88
10,100

test2.csv

read_start,read_end
10,100
100,10
8,10

my question is how do i make a code that can check if all the values in read_start is less than or equal to the ones in read_end return True then return False if any value of read_start is greater than read_end

example:

validate_alignment('test1.csv')

if i test this code it will returnn True

test2.csv will be False

this is what i have tried

import csv

def validate_alignment(alignment_filename):
    file=open(alignment_filename)

    contentss=csv.reader(file)

    for x in contentss:

        if len(x)==0:
            return False
        elif len(x)!=0:
            if x[0]<x[1]:
                return True
            else:
                return False

Solution

  • data = csv.reader(open(alignment_filename,'r'))
    data.next()
    
    print all([row[0]<row[1] for row in data])