4

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()
The Afghan
  • 99
  • 1
  • 7

3 Answers3

5

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.

DRPK
  • 2,023
  • 1
  • 14
  • 27
  • Thank you for your solution, but now it does not split the words line by line. My post is do split words line by line even in ur solution I added the split("\n"). – The Afghan Nov 11 '17 at 19:46
  • 1
    @TheAfghan: so ...plz write your IO sample in your question, i will check it. – DRPK Nov 11 '17 at 19:49
  • I want read words line by line from sentences, when a sentence end with ".", just it should add a newline after "." – The Afghan Nov 11 '17 at 20:07
0

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()
bastelflp
  • 9,362
  • 7
  • 32
  • 67
0

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()
The Afghan
  • 99
  • 1
  • 7
  • "but still I want one more newline after "." "... so...you can use two "\n" characters...like "\n\n" did you try that? – DRPK Nov 13 '17 at 21:40