1

How to print an output file that reads decimal places and spaces in a list of numbers. If it has more or less than 2 decimal places and/or spaces in between the comma and number then the number is invalid and it is printed in the output text file. If it has 2 decimal places and no spaces in between number and comma then it is a Valid number and I is printed in the output text file. Output file: VALID #it is only valid If float has two decimal places and no spaces 10.34,456.78

INVALID #it is only invalid if float has greater or less than two decimal places or it's an integer or if there's a whitespace in between number and comma 10.345, 45.678 (white space & it has 3 sig figs)

Create a file with numbers separated by commas text file:

1,2.12,3.123

1,1.00

How many numbers does it pass the VALID sigfig filters.

from functools import reduce
res = 0
outfile = "output2.txt"
baconFile = open(outfile,"wt")
index = 0
invalid_string = "INVALID"
valid_string = "VALID"
for line in open("file.txt"):               #for loop with variable line defined and using open() function to open the file inside the directory

    for line in open("file.txt"):               #for loop with variable line defined and using open() function to open the file inside the directory

        carrera = ''
        index = index +1                            #adds 1 for every line if it finds what the command wants

        print("Line {}: ".format(index))
        baconFile.write("Line {}:  ".format(index))

    with open('file.txt') as file:
        number_list = file.readline().strip().split(',')
        for line in number_list:
            if len(line.split('.')[-1]) == 2:
                #res += 1
##              print("VALID")

                carrera = valid_string 
            if len(line.split('.')[-1]) != 2:
                #res += 1
                carrera = invalid_string  

            print (carrera)
            baconFile.write(carrera + " ")
#print(res)
baconFile.close()


#For example, my list looks like this from my text file:
1,2.12,3.123
#Then it print out this to my output text file

output (decimal places from each number):
Line 1: INVALID VALID INVALID
#However, if my list from my text file is like this:
1,2.12,3.123
1,1.00
#Then it print out this to my output text file
output: 
Line 1: Line 2:   INVALID 
VALID
INVALID
Line 3: Line 4:   INVALID
VALID
INVALID
#How do I get it to print out like this to my output text file:
Line 1: INVALID VALID INVALID
LINE 2: INVALID VALID
Slashx43
  • 79
  • 11

1 Answers1

3

In your first example:

if '.' in number:
    res += len(number.split('.')[-1])

You're calculating the number of significant digits, then adding that number to res. So of course you end up with the total number of significant digits from all the items.

If you just want to count how many items have exactly two significant digits, try this instead:

if '.' in number:
    if len(number.split('.')[-1]) == 2:
        res += 1
John Gordon
  • 29,573
  • 7
  • 33
  • 58