//Python script to detect CSV column inconsistency

Python script to detect CSV column inconsistency

This is a short script which was used to check the consistency of the number of columns in a csv file.

The script reads the csv file, and checks the number of columns for each row. If there is an inconsistent number, ie the current row has a different number of columns than the previous row, then the program will display ‘not equal’.

Below is a code snippet from the script.


inputFile='test.csv'

infile = open(inputFile, "r")
read = csv.reader(infile)

print("Starting")
#get the number of columns in the first row
previousColumNum = len(next(read))
print("num columns = "+str(previousColumNum))
# check for each row
for row in read:
currentRowNum = len(row)
if currentRowNum != previousColumNum:
print("Not equal")
print(currentRowNum)
previousColumNum=currentRowNum
print("done")