使用open()和.write时,新文件的第一行丢失

时间:2019-06-15 08:00:19

标签: python-3.x

我有一些文本的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 text\n")
            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一些文本”。

1 个答案:

答案 0 :(得分:0)

  • 空字符串为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 text\n")
            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