python - 将文本拆分为章节并将其存储在 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 还很陌生,所以如果您需要更多信息,请告诉我。
解决方案
这对我有用:
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
请注意:这不适用于有序言的书,因为它只会将它们从章节列表中删除。
推荐阅读
- javascript - 更改宽度后元素消失
- r - 如何获取包含 NA OR 值 < 0 的两列矩阵中的行列表?
- matlab - 在 Matlab 中分配带偏差的有符号整数
- javascript - AngularJs 指令在页面上创建固定 div
- wordpress - WordPress:在上传文件夹中删除用户及其文件
- regex - 如何在正则表达式代码中查找和替换
- kotlin - Kotlin:从类的属性生成 Comparable#compareTo() 函数
- r - 如何抓取由Javascript动态生成的表
- oracle - Oracle 中两个模式的权限
- python - 将 Twilio 集成到 Django 应用程序中