How To Run Something On Each Line Of Txt File?
Solution 1:
You do not have to use while
to iterate on each line of file and each digit in line.
You have to use for
loop with enumerate()
builtin function like this:
filename = input("Enter the name of the .txt file: ")
file = open(filename)
lines = file.readlines()
# # for testing# lines = [# '1-2345678-90\n', # invalid# '18-619727-17\n' # valid# ]# iterate on linesfor line in lines:
SUM = 0# remove '\n', new line, from line
line = line.strip()
# remove '-' from line
line = ''.join(digit for digit in line if digit != '-')
# iterate on line's digitsfor i, digit inenumerate(line):
SUM += int(digit) * (len(line) - i)
Mod = SUM % 11# print line for demonstrationprint('ISBN-10: ', line)
# zero remainder = valid ISBNif Mod == 0: # if not Mod:print("Your ISBN-10's are Valid!")
else:
print("Your ISBN-10's are invalid")
will print:
ISBN-10: 1234567890
Your ISBN-10's are invalid
ISBN-10: 1861972717
Your ISBN-10's are Valid!
Solution 2:
You can do the processing on-the-fly as you're reading in the file. For testing I used a file named isbn10.txt
that contained these made-up numbers (after the first, which is valid, every other following number is invalid).
0-201-53082-1
0-20A-53082-1
0-306-40615-2
0-307-40615-9
0-507-40615-X
Here's the code to do it. ISBNs digits are base 11 and can be 0-9 or X, not just 0-9 as with base 10, so that has to be taken care of when doing computations. Also note that the following doesn't save the numbers anywhere, so that would need to be added it you want to keep them around for some reason. It also does different calculations than your code for validating them which is based on information in Wikipedia article on the subject (see the section titled ISBN-10 check digits). There's also a description of how it's used in the ISBN 10 section of Wikipedia article on Check digits.
# Dictionary to map ['0'-'9', 'X', 'x'] to [0-9, 10, 10].
BASE10 = {digit: i for i, digit inenumerate('0123456789X')}
BASE10['x'] = BASE10['X'] # Allow upper and lowercase 'x's.#filename = input("Enter the name of the .txt file: ")
filename = 'isbn10.txt'# Hardcoded for testing.
invalid_numbers_count = 0withopen(filename, "r") as file:
for numbers_count, line inenumerate(file, 1):
# Remove any leading or trailing whitespace and dashes from number.
digits = line.rstrip().replace('-', '')
try:
chksum = sum((i * BASE10[digit]) for i, digit inenumerate(digits, 1))
except KeyError: # Invalid digit.
chksum = 1# Any non-multiple of 11.if chksum % 11 == 0:
print('ISBN {} is valid'.format(line.strip()))
else:
print('ISBN {} is invalid'.format(line.strip()))
invalid_numbers_count += 1print("{} of the {} ISBN-10's are invalid".format(
invalid_numbers_count if invalid_numbers_count else"None",
numbers_count))
Output:
ISBN 0-201-53082-1 is valid
ISBN 0-20A-53082-1 is invalid
ISBN 0-306-40615-2 is valid
ISBN 0-307-40615-9 is invalid
ISBN 0-507-40615-X is valid
2 of the 5 ISBN-10's are invalid
Solution 3:
Let's say:
withopen("isbn_numbers.txt", "r") as fs:
lines = fs.readlines() # will give a list
You can iterate over lines
to compute the sum, or use _sum = sum(map(int, lines))
assuming you have numbers in the txt file.
Instead of using variables here, use the list which you already have and then get the values using index.
Post a Comment for "How To Run Something On Each Line Of Txt File?"