0

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]

Slashx43
  • 79
  • 11
  • how is Line 1, column 3 having 4 whitespaces? Also, shouldn't Line 1 columns 2 and 4 have 2 decimals each? – SanV Jun 20 '19 at 05:03
  • @SanV Ahh you are correct I was not aware of that. I made an update to it thanks for the catch! – Slashx43 Jun 20 '19 at 15:07

1 Answers1

1

UPDATE: simplified if isinstance(eval(x), float) to if '.' in x as suggested by @ImperishableNight in comments below:


You could do something like this:

X = '1,1.02,    123.0005,5.22'
X = X.split(',')
print('Decimals: ', end='')
for i, x in enumerate(X):
    if '.' in x:
        print([i + 1, len(x.split('.')[1])], end='')
    else:
        print([i + 1, 0], end='')

print('')        
print('Whitespaces: ', end='')
for i, x in enumerate(X):
    print([i+1, len(x) - len(x.lstrip(' '))], end='')    

Output:
## Decimals: [1, 0][2, 2][3, 4][4, 2]
## Whitespaces: [1, 0][2, 0][3, 4][4, 0]  
SanV
  • 855
  • 8
  • 16
  • it does work fine but I need it to get the numbers/data from a text file that already has those numbers. – Slashx43 Jun 20 '19 at 15:09
  • [Read each line of numbers from the text file as a string](https://stackoverflow.com/questions/8369219/how-to-read-a-text-file-into-a-string-variable-and-strip-newlines) and then loop over them with code above (or perhaps call it as a function). – SanV Jun 20 '19 at 15:56
  • Why the call to `eval` when all you really want to know is whether the number has a decimal point? – Imperishable Night Jun 20 '19 at 15:59
  • @ImperishableNight The way it is coded, x is a string, e.g., '1.02', so eval is needed to convert it to a number: `eval('1.02') = 1.02`. – SanV Jun 20 '19 at 16:04
  • @ImperishableNight good point. changed code accordingly. thanks. – SanV Jun 21 '19 at 16:39