python 3.x - How do add onto a text file when after writing a code? -


so trying code read infile text line line. has split each line run through check if valid or not. reason reads last line , prints in outfile 00-7141858-x assuming reading each line down there there first. went through processes on last line?

 019-923-3241 818-851-703x 5703781188 031-x287085- 00-7141858-x 

i want outfile 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 = ''     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)     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)     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')     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 = []     in processed_s:         if i.isdigit():             count += int(i)             s2.append(count)         if == 'x':             = '10'             count += int(i)             s2.append(count)     s2_count = 0     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()

almost there. need write file instead of appending though. , don't forget newline.

outfile.write(processed_s) outfile.write(" - invalid\n") 

Comments

Popular posts from this blog

css - Which browser returns the correct result for getBoundingClientRect of an SVG element? -

gcc - Calling fftR4() in c from assembly -

.htaccess - Matching full URL in RewriteCond -