简体   繁体   中英

stop while loop when the text ends

I have a program that loops through the lines of a book to match some tags I've created indicating the start and the end of each chapter of this book. I want to separate each chapter into a different file. The program finds each chapter and asks the user to name the file, then it continues until the next chapter and so on. I don't know exactly where to put my "break" or something that could stop my loop. The program runs well but when it reaches the last chapter it goes back to the first chapter. I want to stop the loop and terminate the program when the tags and the chapters finish and also print something like "End of chapters". Can anyone help me with that? The code is below:

import re
def separate_files ():
    with open('sample.txt') as file:
        chapters = file.readlines()



pat=re.compile(r"[@introS\].[\@introEnd@]")
reg= list(filter(pat.match, chapters))
txt=' '

while True:
    for i in chapters:
        if i in reg:
            print(i)
            inp=input("write text a file? Y|N: ")
            if inp =='Y':
                txt=i
                file_name=input('Name your file: ')
                out_file=open(file_name,'w')
                out_file.write(txt)
                out_file.close()
                print('text', inp, 'written to a file')
            elif inp =='N':
                break
        else:
            continue
    else:
        continue


separate_files()

I think a simpler definition would be

import re
def separate_files ():
    pat = re.compile(r"[@introS\].[\@introEnd@]")

    with open('sample.txt') as file:

        for i in filter(pat.match, file):
            print(i)
            inp = input("write text to a file? Y|N: ")
            if inp != "Y":
                continue

            file_name = input("Name of your file: ")
            with open(file_name, "w") as out_file:
                out_file.write(i)
            print("text {} written to a file".format(i))

Continue the loop as soon as possible in each case, so that the following code doesn't need to be nested more and more deeply. Also, there's no apparent need to read the entire file into memory at once; just match each line against the pattern as it comes up.

You might also consider simply asking for a file name, treating a blank file name as declining to write the line to a file.

for i in filter(pat.match, file):
    print(i)
    file_name = input("Enter a file name to write to (or leave blank to continue: ")
    if not file_name:
        continue

    with open(file_name, "w") as out_file:
        out_file.write(i)
    print("text {} written to {}".format(i, file_name)

I can't run your code but I assume if you remove the

while True:

line it should work fine. This will always be executed as there is nothing checked

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM