如何在特定文本或行上将文本文件拆分为小多个文件?

发布于 2025-02-04 05:11:34 字数 443 浏览 3 评论 0原文

我有书的txt文件,其中每章中都有单词“ - 末尾”。我想将每个章节分为不同的文件,例如 -

Chapter001.txt
Chapter002.txt
Chapter003.txt
.
.
.
ChapterNNN.txt

我在Python3中写下了以下代码。

groups = open('input.txt').read()
groups_divided = groups.split('-THE END-\n')
temp = group.split('\n')

我现在要它将其分为不同的文件,并给他们命名“章节”。 另外,我没有得到如何拆分和创建文件并确保其涵盖所有查询的方法。

另外,请告诉我是否有任何简单的方法可以通过任何软件进行操作。

I have txt file of book which have word "-THE END-" at every chapter. I wanted to split each chapter into different file like-

Chapter001.txt
Chapter002.txt
Chapter003.txt
.
.
.
ChapterNNN.txt

I have written following code in python3.

groups = open('input.txt').read()
groups_divided = groups.split('-THE END-\n')
temp = group.split('\n')

I want now it to split it into different files and give them name "Chapter".
Also I am not getting how to split and create files and ensure that it is covered all queries.

Also please tell me if there is any simple method to do it via any software.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

二手情话 2025-02-11 05:11:34

您可以通过一次处理输入文本文件来进行操作,而不是通过简单地迭代并累积其中的每一行,直到遇到“结尾”行:

with open('input.txt') as inp:
    n = 1
    chapter = []
    for line in inp:
        if line != '-THE END-\n':
            chapter.append(line)
        else:
            filename = f'Chapter{n:03d}.txt'
            with open(filename, 'w') as outp:
                outp.writelines(chapter)
            n += 1
            chapter = []

You could do it by processing the input text file a line at a time instead of reading the whole thing into memory first by simply iterating over and accumulating each line in it until an "end of chapter" line is encountered:

with open('input.txt') as inp:
    n = 1
    chapter = []
    for line in inp:
        if line != '-THE END-\n':
            chapter.append(line)
        else:
            filename = f'Chapter{n:03d}.txt'
            with open(filename, 'w') as outp:
                outp.writelines(chapter)
            n += 1
            chapter = []
顾挽 2025-02-11 05:11:34

另一种方法是查找每个“ - 结尾 - ”的索引,然后使用这些索引创建章节。请参阅以下代码:

with open("my_poem.txt") as f:
    lines = f.readlines()

indices = [0]

for idx, line in enumerate(lines):
    if "-THE END-" in line:
        indices.append(idx) # idx number of line where -THE END- is occurred

chapter_counter = 2

while chapter_counter <= len(indices):
    with open(f"Chapter_{chapter_counter-1}.txt", "a") as w:
        lines_chapter = lines[indices[chapter_counter-2]+1:indices[chapter_counter-1]+1] # From -THE END- to -THE END-
        for line_chapter in lines_chapter:
            w.write(f"{line_chapter}")

    chapter_counter += 1

One other approach is to find indices of each "-THE END-" then create chapters using these indices. See the following code:

with open("my_poem.txt") as f:
    lines = f.readlines()

indices = [0]

for idx, line in enumerate(lines):
    if "-THE END-" in line:
        indices.append(idx) # idx number of line where -THE END- is occurred

chapter_counter = 2

while chapter_counter <= len(indices):
    with open(f"Chapter_{chapter_counter-1}.txt", "a") as w:
        lines_chapter = lines[indices[chapter_counter-2]+1:indices[chapter_counter-1]+1] # From -THE END- to -THE END-
        for line_chapter in lines_chapter:
            w.write(f"{line_chapter}")

    chapter_counter += 1
陪你搞怪i 2025-02-11 05:11:34
n = 0
for chapter in groups_divided:
    with open(f'chapter{n}.txt', 'w') as file: 
       file.write(chapter)
       n += 1
n = 0
for chapter in groups_divided:
    with open(f'chapter{n}.txt', 'w') as file: 
       file.write(chapter)
       n += 1
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文