Suliman Sharif
Suliman Sharif

Reputation: 607

How do add onto a text file when after writing a code?

So I am trying to get the code to read the inFile text line by line. It has to split each line then run through the check if valid or not. For some reason it only reads the last line and then prints in the outFile 00-7141858-X so I am assuming it is reading by each line to get down there there first. But it only went through the processes on the last line?

019-923-3241
818-851-703X
5703781188
031-X287085-
00-7141858-X

I want the outfile to look something like this

019-923-3241 - valid
818-851-703X - invalid
5703781188 - valid
031-X287085- invalid
00-7141858-X - valid

Thanks!

def pre_process (processed_S):
    st = ''
    for ch in processed_S:
       if ch == '-':
           st = st + ''
       else:
           st = st + ch
    return st   

def digit_check (processed_S):
    digit_nums = '0123456789Xx'
    nums = set(digit_nums)
    for ch in processed_S:
        if not ch in nums:
            print ("Invalid ISBN")
    return processed_S

def length_check(processed_S):
    if len(processed_S) < 10 or len(processed_S) > 10:
        print ("Invalid ISBN")

def value_placement (processed_S):
    first_nine = '0123456789'
    nums2 = set (first_nine)
    for ch in range(len(processed_S) - 1):
        if not str(ch) in nums2:
           print ("Invalid ISBN")
    return processed_S

def check_x (processed_S):
    last_letter = '0123456789Xx'
    nums3 = set (last_letter)
    if not str(processed_S[-1]) in nums3:
            print ("Invalid ISBN")
    return processed_S

def main():

    inFile = open ('isbn.txt', 'r')
    outFile = open ('isbnOut.txt', 'w')
    for line in inFile:
        line = line.strip()
    processed_S = pre_process (line)
    st = digit_check (processed_S)
    st2 = length_check (processed_S)
    st3 = value_placement (processed_S)
    st4 = check_x (processed_S)
    count = 0
    s2 = []
    for i in processed_S:
        if i.isdigit():
            count += int(i)
            s2.append(count)
        if i == 'X':
            i = '10'
            count += int(i)
            s2.append(count)
    s2_count = 0
    for j in s2:
        if i.isdigit():
            s2_count += int(j)
    if s2_count % 11 != 0:
        outFile.write(processed_S)
        outFile.write(" - Invalid\")
    else:
        outFile.write(processed_S)
        outFile.write(" - valid\n")

    inFile.close()
    outFile.close()

main()

Upvotes: 0

Views: 114

Answers (3)

Suliman Sharif
Suliman Sharif

Reputation: 607

I don't really remember the solution but this solved it apparently. Just came back to fix the solution.

def pre_process (processed_S):
   st = ''
   for ch in processed_S:
       if ch == '-':
           st = st + ''
       else:
           st = st + ch
   return st   

def digit_check (processed_S):
    digit_nums = '0123456789Xx'
    nums = set(digit_nums)
    for ch in processed_S:
        if not ch in nums:
            print ("Invalid ISBN")
    return processed_S

def length_check(processed_S):
    if len(processed_S) < 10 or len(processed_S) > 10:
        print ("Invalid ISBN")

def value_placement (processed_S):
    first_nine = '0123456789'
    nums2 = set (first_nine)
    for ch in range(len(processed_S) - 1):
        if not str(ch) in nums2:
            print ("Invalid ISBN")
    return processed_S

def check_x (processed_S):
    last_letter = '0123456789Xx'
    nums3 = set (last_letter)
    if not str(processed_S[-1]) in nums3:
            print ("Invalid ISBN")
    return processed_S

def main():

    inFile = open ('isbn.txt', 'r')
    outFile = open ('isbnOut.txt', 'w')
    for line in inFile:
        line = line.strip()
        processed_S = pre_process (line)
        st = digit_check (processed_S)
        st2 = length_check (processed_S)
        st3 = value_placement (processed_S)
        st4 = check_x (processed_S)
        count = 0
        s2 = []
        for i in processed_S:
            if i.isdigit():
                count += int(i)
                s2.append(count)
            if i == 'X':
                i = '10'
                count += int(i)
                s2.append(count)
        s2_count = 0
        for j in s2:
            if i.isdigit():
                s2_count += int(j)
        if s2_count % 11 != 0:
            outFile.write(processed_S)
            outFile.write(" - Invalid\n")
        else:
            outFile.write(processed_S)
            outFile.write(" - valid\n")
    inFile.close()
    outFile.close()

main()

Upvotes: -1

Blckknght
Blckknght

Reputation: 104712

There are several issues with your code. The one you're asking about is due to an indentation issue. Here's the loop in your code that reads from your input file:

for line in inFile:
    line = line.strip()
processed_S = pre_process (line)

This only does the strip on each line, throwing the results away afterwards. The pre_process call is only done after the loop ends, and it only happens on the last value that line was given.

To fix this, you need to indent the processed_S = preprocess (line) line and most of the following ones so that they're at the same level as line = line.strip().

Other issues:

  1. Your various whatever_check functions don't do anything but print if the check fails (and they may print multiple times!). Probably you should make them return a True or False value, rather than printing and returning the inputted string.

  2. digit_check will only fail if one of value_placement or check_x will also fail. It's probably not necessary to have all three (either keep the latter two, or just make one function that tests everything). Something like all(c in digits for c in s[:-1]) and s[-1] in (digits + 'X') would do all the tests in one line.

Other stuff that's not really wrong, but could be improved:

  1. Use the with statements to make sure your files get closed after you're done with them, rather than relying on your own manual close calls.

    with open ('isbn.txt', 'r') as infile, open ('isbnOut.txt', 'w') as outfile:
        # the rest
    
  2. Your loop to add up the ISBN checksum could be simplified:

    checksum = t = 0
    for c in processed_S:
        if c.isdigit():
            t += int(c)
        else: # c must be X, or a previous test should have failed
            t += 10
        checksum += t
    
    if checksum % 11 == 0:
        # valid
    
  3. Probably the checksum test should go into a separate function from main. Furthermore, it and the other tests you have for validity should be called from a higher level valid_isbn function that takes care of the preprocessing and all the checks. This will let main be greatly simplified. Ideally to:

    def main():
        with open ('isbn.txt', 'r') as infile, open ('isbnOut.txt', 'w') as outfile:
        for line in infile:
            if verify_isbn(line):
                outfile.write(line.strip() + " - valid\n")
            else:
                outfile.write(line.strip() + " - invalid\n")
    

Upvotes: 0

Ignacio Vazquez-Abrams
Ignacio Vazquez-Abrams

Reputation: 798576

Almost there. You need to write to the file instead of appending though. And don't forget the newline.

outFile.write(processed_S)
outFile.write(" - Invalid\n")

Upvotes: 2

Related Questions