0

所以我试图让代码逐行读取 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()

主要的()

4

3 回答 3

2

差不多好了。您需要写入文件而不是追加。并且不要忘记换行符。

outFile.write(processed_S)
outFile.write(" - Invalid\n")
于 2013-08-09T23:52:31.700 回答
0

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")
    
于 2013-08-10T01:42:55.273 回答
-1

我真的不记得解决方案,但这显然解决了它。刚回来修复解决方案。

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()
于 2013-08-10T00:29:56.030 回答