Find a dot in a text file and add a newline to the file in Python?

Viewed 3164

I read from a file, if it finds a ".", it should add a newline "\n" to the text and write it back to the file. I tried this code but still have the problem.

inp = open('rawCorpus.txt', 'r')
out = open("testFile.text", "w")

for line in iter(inp):
    l = line.split()
    if l.endswith(".")
       out.write("\n")
    s = '\n'.join(l)
print(s)
out.write(str(s))
inp.close()
out.close()
3 Answers

Try This ( Normal way ):

with open("rawCorpus.txt", 'r') as read_file:
    raw_data = read_file.readlines()


my_save_data = open("testFile.text", "a")

for lines in raw_data:

    if "." in lines:

        re_lines = lines.replace(".", ".\r\n")
        my_save_data.write(re_lines)

    else:
        my_save_data.write(lines + "\n")

my_save_data.close()

if your text file is not big you can try this too :

with open("rawCorpus.txt", 'r') as read_file:
    raw_data = read_file.read()

re_data = raw_data.replace(".", ".\n")

with open("testFile.text", "w") as save_data:
    save_data.write(re_data)

UPDATE ( output new lines depends on your text viewer too! because in some text editors "\n" is a new line but in some others "\r\n" is a new line. ) :

input sample :

This is a book. i love it.

This is a apple. i love it.

This is a laptop. i love it.

This is a pen. i love it.

This is a mobile. i love it.

Code:

last_buffer = []
read_lines = [line.rstrip('\n') for line in open('input.txt')]

my_save_data = open("output.txt", "a")


for lines in read_lines:

    re_make_lines = lines.split(".")

    for items in re_make_lines:

        if items.replace(" ", "") == "":
            pass

        else:

            result = items.strip() + ".\r\n"
            my_save_data.write(result)

my_save_data.close()

Ouput Will Be :

This is a book.

i love it.

This is a apple.

i love it.

This is a laptop.

i love it.

This is a pen.

i love it.

This is a mobile.

i love it.

You are overwriting the string s in every loop with s = '\n'.join(l).

Allocate s = '' as empty string before the for-loop and add the new lines during every loop, e.g. with s += '\n'.join(l) (short version of s = s + '\n'.join(l)

This should work:

inp = open('rawCorpus.txt', 'r')
out = open('testFile.text', 'w')

s = ''  # empty string

for line in iter(inp):
    l = line.split('.')
    s += '\n'.join(l)  # add new lines to s

print(s)
out.write(str(s))

inp.close()
out.close()

Here is my own solution, but still I want one more newline after ".", that this solution not did this read_lines = [line.rstrip('\n') for line in open('rawCorpus.txt')] words = []

my_save_data = open("my_saved_data.txt", "w")

   for lines in read_lines:

   words.append(lines)


for word in words:
   w = word.rstrip().replace('.', '\n.')
   w = w.split()
   my_save_data.write(str("\n".join(w)))
   print("\n".join(w))

my_save_data.close()
Related