简体   繁体   中英

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.

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, 'w') will overwrite the file every time it starts. My guess is that what's currently happening is that only the last element in sentenceSplit is showing up in myfile .

The simple "solution" is to use append instead of 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? 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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