I'm going to be reading thousands of lines and columns that have numbers and I need to see if they pass the VALID or INVALID test. If a number has more or less than two decimal places and has a space in between numbers and commas then it is an INVALID number. If a number has two decimal places and no spaces in between the numbers and commas then it is VALID. I need a code that can display what row and column they are at.
I've already created a code where it states which numbers are VALID/INVALID.
#Open the files
with open('file.txt') as fp:
#Extract out non-empty lines from file
lines = [line for line in fp.readlines() if line.strip()]
res = []
#Iterate over the lines
for idx, line in enumerate(lines):
#Number is valid if it doesn't start with a whitespace, has a decimal part and the decimal part is two digits long
res = ['VALID' if not item.startswith(' ') and '.' in item and len(item.split('.')[1]) == 2 else 'INVALID' for item in line.split(',')]
#Print the result
print("Line {}: {}".format(idx+1, ' '.join(res)))
My text file has the following numbers:
Line1: 1,1.02, 123.0005,5.22
Line2: 2,2.02,1.123
Line3: 2,5.01,2.02,10,11,12,13.22
My Output is this:
Line1: INVALID VALID INVALID INVALID
Line2: INVALID VALID INVALID
Line3: INVALID VALID VALID INVALID INVALID INVALID INVALID
I need it to print out like this now:
LineN: Decimals:[column,amount of decimals] WhiteSpace:[column,amount of whitespaces]
Line1: Decimals:[1,0][2,2][3,4][4,2] whitespace:[1,0][2,0][3,1][4,0]
Line2: Decimals:[1,0][2,2][3,3] whitespace:[1,0][2,0][3,0]
Line3: Decimals:[1,0][2,2][3,2][4,0][5,0][6,0][7,2] whitespace:[1,0][2,0][3,0][4,0][5,0][6,0][7,0]