【问题标题】:Not overwriting text file properly (Python)没有正确覆盖文本文件(Python)
【发布时间】:2018-04-25 20:15:48
【问题描述】:

我的程序应该获取一个文本文件,打印内容,计算行数,要求输入字符串,删除该字符串的每一次出现,说出该字符串被删除的次数,然后输出新的内容文件。我们被要求使用一个文件,而不是输入和输出文件。

我的代码可以正常工作并完成它应该做的所有事情,直到我尝试将更改存储在 char_count 函数末尾的文件中,而 print_output 函数似乎根本无法正常工作。

如果我有一个内容输入文件:

Apples
Oranges
Bananas
Apples
Oranges
Bananas

如果我尝试删除 Bananas,则输入文件的结果文件内容为:

ApplesOrangesApplesOrangesles
Oranges
Bananas

我一直在试图弄清楚发生了什么没有进展,我们的课程教科书似乎没有提到覆盖输入文件,但我们需要为作业这样做。我的最后两个函数有什么问题?

def main():

    input_file_name = input("Please Enter the name of your text file: ")
    infile = open(input_file_name, "r+")
    print()

    print("---------------------------------------")
    print("THE FILE CONTENTS ARE")
    print("---------------------------------------")
    print_file(infile)
    print("---------------------------------------")
    count_lines(infile)
    print("---------------------------------------")
    input_string = input("Please enter the word or string of words you want to remove from the text file: ")
    print("---------------------------------------")
    char_count(infile, input_string)
    print("---------------------------------------")
    print("THE NEW FILE CONTENTS ARE")
    print_output(infile)
    print("---------------------------------------")

    infile.close()

def print_file(infile):
    infile.seek(0)
    allLines = infile.readlines()
    for line in allLines:
        text = line.rstrip()
        print(text)

def count_lines(infile):
    infile.seek(0)
    allLines = infile.readlines()
    count = 0
    char = " "
    for line in allLines :
        text = line.rstrip()
        while char != "":
            char = infile.read(1)
        count = count + 1
    print("THE NUMBER OF LINES IS: %d " % count)

def char_count(infile, input_string) :
    count = 0
    infile.seek(0)
    allLines = infile.readlines()
    infile.seek(0)
    for line in allLines:
        while input_string in line:
            line = line.replace(input_string, "")
            count = count + 1
        text = line.rstrip()
        infile.write(text)
    print("NUMBER OF OCCURRENCES REMOVED IS: %d" % count)

def print_output(infile):
    infile.seek(0)
    allLines = infile.readlines()
    for line in allLines:
        text = line.rstrip()
        print(text)
main()

【问题讨论】:

    标签: python function readlines


    【解决方案1】:

    您必须先截断文件才能获得所需的输出。

    def main():
    
        input_file_name = input("Please Enter the name of your text file: ")
        infile = open(input_file_name, "r+")
        print()
    
        print("---------------------------------------")
        print("THE FILE CONTENTS ARE")
        print("---------------------------------------")
        print_file(infile)
        print("---------------------------------------")
        count_lines(infile)
        print("---------------------------------------")
        input_string = input("Please enter the word or string of words you want to remove from the text file: ")
        print("---------------------------------------")
        char_count(infile, input_string)
        print("---------------------------------------")
        print("THE NEW FILE CONTENTS ARE")
        print_output(infile)
        print("---------------------------------------")
    
        infile.close()
    
    def print_file(infile):
        infile.seek(0)
        allLines = infile.readlines()
        for line in allLines:
            text = line.rstrip()
            print(text)
    
    def count_lines(infile):
        infile.seek(0)
        allLines = infile.readlines()
        count = 0
        char = " "
        for line in allLines :
            text = line.rstrip()
            while char != "":
                char = infile.read(1)
            count = count + 1
        print("THE NUMBER OF LINES IS: %d " % count)
    
    def char_count(infile, input_string) :
        count = 0
        infile.seek(0)
        allLines = infile.readlines()
        infile.seek(0)
        infile.truncate() #Empty your file first to rewrite it
        for line in allLines:
            while input_string in line:
                line = line.replace(input_string, "")
                count = count + 1
            text = line.rstrip() 
            if(text != ""):
                infile.write(text + "\n") #To write in multiple lines
        print("NUMBER OF OCCURRENCES REMOVED IS: %d" % count)
    
    def print_output(infile):
        infile.seek(0)
        allLines = infile.readlines()
        for line in allLines:
            text = line.rstrip()
            print(text)
    main()
    

    【讨论】:

    • 非常感谢!我完全忘了在 char_count 的末尾添加一个文本条件,我之前有 .truncate() 但它在错误的地方。
    • 欢迎您,如果您对答案感到满意,请不要忘记接受答案
    猜你喜欢
    • 1970-01-01
    • 2014-06-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-07-13
    • 2012-07-23
    • 1970-01-01
    相关资源
    最近更新 更多