Understanding the chapter-by-chapter txt.file export and file name settings using for loop (Python)

Asked 2 years ago, Updated 2 years ago, 33 views

I'm a beginner at Python and started taking introductory courses at universities overseas.
It's a totally personal situation, but I'm at a loss for the autumn break without knowing the solution to the problem below...
I would appreciate it if you could give me some advice to help me solve the problem.

Overview
①Each chapter in the txt file is individually truncated into a txt. file in the form of a given file name and serial number.
②In addition to the process of の above, the title name of each chapter is reflected in the file name

条件Conditions 】
·Create source code without hard code
·The file name should be Dracula-Chapter-#.txt.
Example) "Dracula-Chapter-1.txt" in Chapter 1 and "Dracula-Chapter-2.txt" in Chapter 2

[Details of what you want to realize]
①Separate chapters with serial txt. files
"·Each chapter covers the lines that begin with ""CHAPTER...●●"" in each file until the last line of the chapter."
Example) In the first chapter, it ends with "...sky."
*The final chapter covers ~THE END

(2) In addition to the process of の above, the title of each chapter is reflected in the file name
"·Refer to the title of each chapter in the opening table of contents of ""dracula.txt"" and reflect it in the file name of の above."
Example) "Dracula-Chapter-1_Jonathan_Harker's_Journal.txt" in Chapter 1, "Dracula-Chapter-2_Letters -- Lucy_and_Mina.txt" in Chapter 5.
*The space used in the title name in the txt file should be converted to an underbar when the file name is reflected (_)

[File used for assignment]
The "dracula.txt" file stored here
*This is the original file of the English e-book.

】Notes <
Text Source
Project Gutenberg

[Editing code]
The pattern of each chapter starts with "CHAPTER...●●", and the end of each chapter has a new line code of 5 lines except for the last chapter.
The code being edited has passed through the process of separating by referring to the new line code.
I think the space between the chapters is enough to separate the chapters.
Repeat that for 27 chapters, and you're stuck specifying a filename...

fileio=open('dracula.txt','r')
dracula=fileio.read()
outfile=open('Dracula-Chapter-1.txt', 'w')

list_of_drac=dracula.split('\n\n\n\n')
chapters = list_of_drac [4-33]

print(chaptors)#print(chaptors, file=outfile)<Use common outver.when printing>

#in_file.close()
# outfile.close()

python python3

2022-09-30 10:19

1 Answers

Write the code using one of the solutions.As it is homework, I will leave the title for self-resolution.

with open('dracula.txt', 'r') asf:
    dracula=f.read()
list_of_drac=dracula.split('\n\n\n\n')
n = 0
for chapter in list_of_drac:
    if chapter.startswith('CHAPTER'):
        # Get the title on line 3
        n + = 1
        filename = 'Dracula-Chapter-' + str(n) + '.txt'
        with open(filename, 'w') as f:
            f.write(chapter)


2022-09-30 10:19

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.