2017-11-13 15 views
0

내 프로그램은 텍스트 파일을 가져 와서 내용을 인쇄하고 줄 수를 계산하고 문자열 입력을 요청하고 해당 문자열의 모든 항목을 제거하고 문자열이 제거 된 횟수를 말합니다 , 파일의 새로운 내용을 출력 할 수 있습니다. 입력 파일과 출력 파일이 아닌 하나의 파일을 사용하도록 요청 받았습니다.텍스트 파일을 제대로 덮어 쓰지 않음 (파이썬)

내 코드가 작동하고 char_count 함수의 끝에 파일의 변경 사항을 저장하려고 할 때까지 내 모든 코드가 작동합니다. print_output 함수가 제대로 작동하지 않는 것 같습니다.

나는 내용의 입력 파일이있는 경우 :

Apples 
Oranges 
Bananas 
Apples 
Oranges 
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() 

답변

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

고마워요! 나는 char_count의 끝에 텍스트를위한 조건을 넣는 것을 완전히 잊었다. 그리고 나는 이전에 거기에서 .truncate()를했다. 그러나 그것은 틀린 장소에 있었다. –

+0

귀하의 환영이며, 답변에 만족한다면 답변을 수락하는 것을 잊지 마십시오. –