使用 open() 和 .write 时缺少新文件中的第一行



我有一些带有文本的txt文件。从这个文件中,我尝试获取我需要的内容并将其保存到新文件中。如果我将所有内容保存到一个文件中,它就可以工作。但是,如果我尝试将其保存到几个文件中,那么新文件中第一行的第一部分就会丢失。

file_out = open("Words 01-24.txt", "w")
with open("raw.txt", "r") as file_in:
    new_line = file_in.readline()
    index = 0
    while new_line:
        if "dictionary" in new_line:
            file_out.write(f"some text {index} ")
        elif "definition" in new_line:
            file_out.write("some textn")
            index += 1
        if index % 25 == 0 and index != 0:
            file_out.close()
            file_out = open(f"Words {index}-{index + 24}.txt", "w")
        new_line = file_in.readline()
file_out.close()

在新文件中,第一行是"一些文本"而不是"一些文本 25 一些文本"。

  • 空字符串是False的 - 所以如果你的第一行为空,它不会进入你的 while 循环。

  • 如果'dictionary''definition'都不在非空行中,则不会写入任何内容。

我可能会将代码更改为:

fn = "raw.txt"
names =["Words 01-24.txt"]    
file_out = open(names[-1], "w")
with open(fn) as file_in: 
    index = 0
    for new_line in file_in:
        if "dictionary" in new_line:
            file_out.write(f"some text {index} ")
        elif "definition" in new_line:
            file_out.write("some textn")
            index += 1
            file_used = True
        elif not new_line.strip():  # only spaces or r or n or other whitespaces
            print("empty line") 
        else:
            print(f"line with other content: {new_line}")
        # only create a new file if the current one was already 
        # written into
        if index % 25 == 0 and index != 0 and file_used:
            file_out.close()
            names.append(f"Words {index}-{index + 24}.txt")
            file_used = False
            file_out = open(names[-1], "w")    
file_out.close()
for n in names:
    print(n, open(n).read(), sep="n================n")

(文件(输出:

Words 01-24.txt
================
some text 0 some text
some text 1 some text
some text 2 some text
some text 3 some text
some text 4 some text
some text 5 some text
some text 6 some text
some text 7 some text
some text 8 some text
some text 9 some text
some text 10 some text
some text 11 some text
some text 12 some text
some text 13 some text
some text 14 some text
some text 15 some text
some text 16 some text
some text 17 some text
some text 18 some text
some text 19 some text
some text 20 some text
some text 21 some text
some text 22 some text
some text 23 some text
some text 24 some text
Words 25-49.txt
================
some text 25 some text
some text 26 some text
some text 27 some text

最新更新