2017-11-13 12 views
0

Mein Programm soll eine Textdatei nehmen, den Inhalt drucken, die Zeilen zählen, nach einer Zeichenketteneingabe fragen, jedes Vorkommen dieser Zeichenkette entfernen, sagen, wie oft die Zeichenkette entfernt wurde , und geben Sie dann den neuen Inhalt der Datei aus. Wir wurden gebeten, eine Datei zu verwenden, keine Eingabe- und Ausgabedatei.Textdatei nicht richtig überschreiben (Python)

Mein Code funktioniert und tut alles, was es soll, bis ich versuche, die Änderungen in der Datei am Ende der Funktion char_count zu speichern, und die Funktion print_output scheint überhaupt nicht zu funktionieren.

Wenn ich eine Eingabedatei aus dem Inhalt:

Apples 
Oranges 
Bananas 
Apples 
Oranges 
Bananas 

wenn ich versuche, Bananen, die resultierenden Dateiinhalte für die Eingabedatei zu entfernen ist:

ApplesOrangesApplesOrangesles 
Oranges 
Bananas 

Ich habe versucht, zu herauszufinden, was ohne Fortschritte vor sich geht, und unser Kursbuch scheint nicht zu erwähnen, dass Eingabedateien überschrieben werden, aber wir müssen es für eine Aufgabe tun. Was ist falsch an meinen letzten beiden Funktionen?

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() 

Antwort

1

Sie müssen die Datei zuerst abschneiden, um die erforderliche Ausgabe zu erhalten.

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() 
+0

Vielen Dank! Ich habe komplett vergessen, am Ende von char_count eine Bedingung für den Text einzugeben, und ich hatte zuvor truncate() dort, aber es war an der falschen Stelle. –

+0

Ihre Begrüßung, und vergessen Sie nicht, die Antwort zu akzeptieren, wenn Sie mit der Antwort zufrieden sind –

Verwandte Themen