in Education by
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() JavaScript questions and answers, JavaScript questions pdf, JavaScript question bank, JavaScript questions and answers pdf, mcq on JavaScript pdf, JavaScript questions and solutions, JavaScript mcq Test , Interview JavaScript questions, JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)

1 Answer

0 votes
by
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)

Related questions

0 votes
    What would be your preferred way to concatenate strings from a sequence such that between every two consecutive ... for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 25, 2022 in Education by JackTerrance
0 votes
    I was supposed to do this exercise: Write a program in C to print a frequency chart on the screen ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 9, 2022 in Education by JackTerrance
0 votes
    I wrote javascript code to solve the following problem >>> Write a function exponentiate that accepts two ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jun 5, 2022 in Education by JackTerrance
0 votes
    When we choose for loop and how it is deffernt from while loop Select the correct answer from above options...
asked Dec 25, 2021 in Education by JackTerrance
0 votes
    WHILE…LOOP is used when we know the number of iterations of a loop. True False Select the correct answer from above options...
asked Nov 27, 2021 in Education by JackTerrance
0 votes
    In the following program, if I set the variable $foo to the value 1 inside the first if statement, ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 17, 2022 in Education by JackTerrance
0 votes
    I have the automata S'-> S S -> a | XbY X -> ε | aZ | Y Y -> b | XX z ... JavaScript questions, JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 3, 2022 in Education by JackTerrance
0 votes
    I'm not sure why this SQL query is not working. I'm new to SQL/PHP so please forgive. ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jan 13, 2022 in Education by JackTerrance
0 votes
    I'm not sure why this SQL query is not working. I'm new to SQL/PHP so please forgive. ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jan 13, 2022 in Education by JackTerrance
0 votes
    If I use a while loop for my below code, it is not giving the desired output, but when i use for loop i am anle ... x += 1 print(Comm) Select the correct answer from above options...
asked Jan 9, 2022 in Education by JackTerrance
0 votes
    convert the following into For loop; while(true) System.out.print(‘*’); (in java ) PLEASE ANSWER FAST… WILL MARK BRAINLIEST** Select the correct answer from above options...
asked Dec 31, 2021 in Education by JackTerrance
0 votes
    write a program to input a number and print table of the number using while loop Select the correct answer from above options...
asked Dec 31, 2021 in Education by JackTerrance
0 votes
    The while loop repeats a set of code while the condition is not met? (a) True (b) False I had ... java programming questions and answers pdf, java interview questions for beginners...
asked Oct 26, 2021 in Education by JackTerrance
...