首页 > 解决方案 > 将文本拆分为章节并将其存储在 txt 文件中

问题描述

我有一个 .txt 文件book.txt,我想将它分成多个 .txt 文件(每章一个)。我希望最终拥有名称引用这些章节的 .txt 文件。例如,如果book.txt有 20 章,我会以1.txt,2.txt等结尾。

这是我到目前为止所拥有的:

import re
book = #I don't know how to import the book.txt file here

chapters = re.split("Chapter ", book, flags = re.IGNORECASE)
for chapter in chapters:
    #Code to write a new .txt file with each element from the list I created using the number after 'Chapter' as the name for the .txt file.

如您所见,我一直在导入book.txt文件和创建新的 txt 文件。我对 Python 还很陌生,所以如果您需要更多信息,请告诉我。

标签: python

解决方案


这对我有用:

import re
book = open("book.txt", "r") #Here we open the book, in this case it is called book.txt
book = str(book.read()) #this is now assigning book the value of book.txt, not just the location
chapters = re.split("Chapter ", book, flags = re.IGNORECASE) #Finds all the chapter markers in the book and makes a list of all the chapters
chapters.pop(0) # Removes the first item in list as this is ""
for i in range(1, len(chapters)+1): #Loops for the number of chapters in the book, starting at chapter 1
    writeBook = open("{}.txt".format(i), "w+") #Opens a book with the name of i, if it does not exist, it creates one
    writeBook.write(chapters[i-1]) #Overwrites what is written in the book with the same chapter in the list
    writeBook.close() #Finally, it closes the text file

请注意:这不适用于有序言的书,因为它只会将它们从章节列表中删除。


推荐阅读