【问题标题】:Reading input from text file and storing the output从文本文件中读取输入并存储输出
【发布时间】:2015-04-08 19:44:08
【问题描述】:

我创建了一个用于验证 ISBN 10 的函数。如果输入来自用户,它就可以很好地工作。这还不够,我想从文本文件中输入并将输出存储在文本文件中。

    print("ISBN-Validation Program")
    isbn = input("Enter isbn: ")

    def stripDashes(isbn):
        sisbn = isbn.replace("-", "").replace(" ", "").upper();

        return checkFormat(sisbn)

    def checkFormat(isbn):
        if len(isbn) == 10 and isbn[:9].isdigit()\
           and (isbn[-1] == "x" or isbn[-1] == "X" or isbn[-1].isdigit()):
            return isValiDisbn(isbn)
        else:
            print("ISBN is not properly formatted.")

    def isValiDisbn(isbn):
        if (isbn[-1] == "x" or isbn[-1] == "X"):
            total = int(isbn[0])*10 + int(isbn[1])*9 + int(isbn[2])*8\
                + int(isbn[3])*7 + int(isbn[4])*6 + int(isbn[5])*5\
                + int(isbn[6])*4 + int(isbn[7])*3 + int(isbn[8])*2 + 10
        else:
            total = int(isbn[0])*10 + int(isbn[1])*9 + int(isbn[2])*8\
                + int(isbn[3])*7 + int(isbn[4])*6 + int(isbn[5])*5\
                + int(isbn[6])*4 + int(isbn[7])*3 + int(isbn[8])*2 + int(isbn[9])

        if total % 11 == 0:
            print("The number is valid.")
        else:
            print("The number is not valid.")

    stripDashes(isbn)

上面的函数适用于用户输入,我猜我的调用函数和在文本文件中打印的代码在某处是错误的。

    def main():
        inFile = open("isbn.txt", "r")
        outFile = open("isbnOut.txt", "a")

        for line in open("isbn.txt", "r"):
            isbns = line.split()
            for isbn in isbns: 
                if checkFormat(isbn) == False:
                    outFile.write(isbn.strip()+"\nISBN is not properly formatted.\n")
                if isValiDisbn(isbn) == True:
                    outFile.write(isbn.strip()+"\nThe number is valid.\n")
                if isValiDisbn(isbn) == False:
                    outFile.write(isbn.strip()+"\nThe number is not valid.\n")

        inFile.close()
        outFile.close()

    def stripDashes(isbn):
        sisbn = isbn.replace("-", "").replace(" ", "").upper();

        return checkFormat(sisbn)

    def checkFormat(isbn):
        if len(isbn) == 10 and isbn[:9].isdigit() and (isbn[-1] == "x" or isbn[-1] == "X" or isbn[-1].isdigit()) == True:
            return isValiDisbn(isbn)
        else:
            return False
            #print("ISBN is not properly formatted.")

    def isValiDisbn(isbn):
        if (isbn[-1] == "x" or isbn[-1] == "X"):
            total = int(isbn[0])*10 + int(isbn[1])*9 + int(isbn[2])*8 + int(isbn[3])*7\
                + int(isbn[4])*6 + int(isbn[5])*5 + int(isbn[6])*4 + int(isbn[7])*3\
                + int(isbn[8])*2 + 10
        else:
            total = int(isbn[0])*10 + int(isbn[1])*9 + int(isbn[2])*8 + int(isbn[3])*7\
                + int(isbn[4])*6 + int(isbn[5])*5 + int(isbn[6])*4 + int(isbn[7])*3\
                + int(isbn[8])*2 + int(isbn[9])

        if total % 11 == 0:
            return True
            #print("The number is valid.")
        else:
            return False
            #print("The number is not valid.")

    main()

谁能告诉我这里出了什么问题并帮助我解决这个问题?

【问题讨论】:

  • 不确定这是否与您的问题有关,但您不应两次致电open("isbn.txt", "r")

标签: function validation python-3.x file-io


【解决方案1】:

问题是您忘记调用stripDashes,因此ISBN 以0-306-40615-2 的形式输入int,而不是0306406152

def stripDashes(isbn):
    sisbn = isbn.replace("-", "").replace(" ", "").upper();

    return sisbn

作为循环的一部分,请执行以下操作:

def main():
    inFile = open("isbn.txt", "r")
    outFile = open("isbnOut.txt", "a")

    for line in open("isbn.txt", "r"):
        isbns = line.split()
        for isbn in isbns:
            stripped_isbn = stripDashes(isbn)
            if checkFormat(stripped_isbn) == False:
                outFile.write(isbn.strip()+"\nISBN is not properly formatted.\n")
            if isValiDisbn(stripped_isbn) == True:
                outFile.write(isbn.strip()+"\nThe number is valid.\n")
            if isValiDisbn(stripped_isbn) == False:
                outFile.write(isbn.strip()+"\nThe number is not valid.\n")

效果很好:

$ cat isbn.txt 
0-306-40615-2
$ python isbn.py 
$ cat isbnOut.txt 
0306406152
The number is valid.

其他注意事项:

  • 您以inFile = open("isbn.txt", "r") 的身份打开in-file,然后在循环for line in open("isbn.txt", "r"): 中重新打开它。只需使用您打开的文件,如 for line in inFile: 或删除第一行。
  • 您执行输出文件写入的代码已损坏。

    if checkFormat(isbn) == False:
        outFile.write(isbn.strip()+"\nISBN is not properly formatted.\n")
    if isValiDisbn(isbn) == True:
        outFile.write(isbn.strip()+"\nThe number is valid.\n")
    if isValiDisbn(isbn) == False:
        outFile.write(isbn.strip()+"\nThe number is not valid.\n")
    

    如果checkFormat 返回False,它仍然会检查ISBN 的有效性,因此elif 应该用于以下行。 isValiDisbn(isbn) == False 也不需要,因为它调用了两次isValiDisbn,并将其与False 进行比较(只需使用not),所以只需使用else

    if checkFormat(isbn) == False:
        outFile.write(isbn.strip()+"\nISBN is not properly formatted.\n")
    elif isValiDisbn(isbn) == True:
        outFile.write(isbn.strip()+"\nThe number is valid.\n")
    else:
        outFile.write(isbn.strip()+"\nThe number is not valid.\n")
    

【讨论】:

  • 非常感谢,它成功了。但是,如果我需要像 0-471-58719-1 这样的输出,我该怎么办?这个数字无效。 14-A123490-3 ISBN 格式不正确。 0-32-108599-X 号码有效。我的意思是破折号是原始的。现在我得到它没有破折号。
  • @saroj 如果这可行,请accept 这个答案,以便其他用户知道这个问题已经解决。谢谢!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-08-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多