0

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?

ATMA
  • 1,450
  • 4
  • 23
  • 33
  • Possible duplicate of [How do I read a text file into a string variable in Python](https://stackoverflow.com/questions/8369219/how-do-i-read-a-text-file-into-a-string-variable-in-python) – emsimpson92 Jul 19 '18 at 16:08
  • 1
    Your loop truncates the file every time it opens it. Why are you reopening the file *at all*, rather than opening it once before the loop? – chepner Jul 19 '18 at 16:15
  • if you use readlines() it read all the line in the file and save them in a list, if you want read line to line use readline() or you read everything and then you make the loop on the list – Carlo 1585 Jul 19 '18 at 16:24

3 Answers3

2

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()
Ravinder Baid
  • 395
  • 1
  • 15
1

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.

NiGiord
  • 57
  • 6
1

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.

Green Cloak Guy
  • 23,793
  • 4
  • 33
  • 53