所以我试图让代码逐行读取 inFile 文本。它必须拆分每一行,然后通过检查是否有效。出于某种原因,它只读取最后一行,然后在 outFile 00-7141858-X 中打印,所以我假设它正在读取每一行以首先到达那里。但它只经历了最后一行的过程?
019-923-3241 818-851-703X 5703781188 031-X287085- 00-7141858-X
我希望 outfile 看起来像这样
019-923-3241 - 有效 818-851-703X - 无效 5703781188 - 有效 031-X287085-无效 00-7141858-X - 有效
谢谢!
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()
主要的()