简体   繁体   English

将文本文件保存在for循环中

[英]Saving text file in a for loop

I'm trying to loop through a file, strip the sentences into individual lines, and then export that data. 我正在尝试遍历文件,将句子剥离成单独的行,然后导出该数据。

filename = '00000BF8_ar.txt'

with open(filename, mode="r") as outfile:   
    str_output = outfile.readlines()
    str_output = ''.join(str_output)
    sentenceSplit = filter(None, str_output.split("."))

    for s in sentenceSplit:
        print(s.strip() + ".")
        #output += s 



        myfile = open(filename, 'w')
        myfile.writelines(s)
        myfile.close()

Unfortunately, it looks like the loop only goes through a few lines and saves them. 不幸的是,看起来循环仅经历了几行并保存了它们。 So the whole file isn't looped through and saved. 因此,整个文件不会循环浏览并保存。 Any help on how I can fix that? 我如何解决该问题有任何帮助吗?

Here is the code I hope this is what you want to achieve, 这是我希望这是您想要实现的代码,

filename = '00000BF8_ar.txt'

with open(filename, mode="r") as outfile:   
    str_output = outfile.readlines()
    str_output = ''.join(str_output)
    sentenceSplit = filter(None, str_output.split("."))
    l=[]

    for s in sentenceSplit:
        l.append(s.strip() + ".")
    myfile = open(filename, 'w')
    myfile.write('\n'.join(l))
    myfile.close()

Each time you re-open the file with the 'w' option, you basically erase its content. 每次使用'w'选项重新打开文件时,基本上都将擦除其内容。

Try modifying your code like this: 尝试像这样修改代码:

filename = '00000BF8_ar.txt'

with open(filename, "r") as infile:
    str_output = infile.readlines()

str_output = ''.join(str_output)
sentenceSplit = filter(None, str_output.split("."))

with open(filename, "w") as outfile:
    for s in sentenceSplit:
        print(s.strip() + ".")
        #output += s 
        s.writelines(s)

Another way to achieve the same thing would have been to open a new file using open(filename_new, 'a') which open a file for appending, but as a rule of thumb try not to open/close files inside a loop. 实现相同目的的另一种方法是使用open(filename_new, 'a')打开一个新文件open(filename_new, 'a')该文件会打开一个文件以进行追加,但是根据经验,请尽量不要在循环内打开/关闭文件。

open(filename, 'w') will overwrite the file every time it starts. open(filename, 'w')每次启动时都会覆盖该文件。 My guess is that what's currently happening is that only the last element in sentenceSplit is showing up in myfile . 我的猜测是,当前正在发生的事情是myFile中只显示了sentenceSplit myfile的最后一个元素。

The simple "solution" is to use append instead of write : 简单的“解决方案”是使用append而不是write

open(filename, 'a')

which will simply start writing at the end of the file, without deleting the rest of it. 它将仅在文件末尾开始写入,而不会删除其余部分。

However, as @chepner's comment states, why are you reopening the file at all? 然而,由于@ chepner的评论状态,你为什么要重新打开该文件呢? I would recommend changing your code to this: 我建议将您的代码更改为此:

with open(filename, mode="r") as outfile:   
    str_output = outfile.readlines()
    str_output = ''.join(str_output)
    sentenceSplit = filter(None, str_output.split("."))

with open(filename, mode='w') as myfile:
    for s in sentenceSplit:
        print(s.strip() + ".")
        myfile.writelines(s)

This way, instead of opening it many times, and overwriting it every time, you're only opening it once and just writing to it continuously. 这样,您不必打开多次并每次覆盖它,只需打开一次并连续写入即可。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM