我很难获得正确的代码来从这本电子书中解析出各个章节,然后将27个章节打印出到自己的文本文件中。我走得最远的是打印“ CHAPTER-1.txt”。我不想硬编码任何东西,也不确定我完全错过了哪里。
infile = open('dracula.txt','r')
readlines = infile.readlines()
toc_list = readlines[74:185]
toc_text_lines = []
for line in toc_list:
if len(line) > 1:
stripped_line = line.strip()
toc_text_lines.append(stripped_line)
#print(len(toc_text_lines))
chaptitles = []
for text_lines in toc_text_lines:
split_text_line = text_lines.split()
if split_text_line[-1].isdigit():
chaptitles.append(text_lines)
#print(len(chaptitles))
print(chaptitles)
infile.close()
import re
with open('dracula.txt') as f:
book = f.readlines()
while book:
line = book.pop(0)
if "CHAPTER" in line and book.pop(0) == '\n':
for title in chapters_names_list: ['CHAPTER I.','CHAPTER II.','CHAPTER III.']
with open("{}.txt".format(chapters_names_list),'w') :