1

I am looking for a way to copy a txt file and modify it so that every sentence begins a new line.

I am very new to programming in Python and would like some help explaining how can I do this.

For example,

First sentence. Second. Third.

Should be

First sentence.

Second.

Third.

This is what I have so far:

f=open('a.txt')  
f1=open('b.txt','a')
with open('a.txt', 'r') as file:
    string = file.read().replace('.', '.\n')
with open('b.txt', 'w') as b:
    b.write(string)
f1.close()
f.close()
Hemal
  • 67
  • 1
  • 7
  • 1
    If you are always splitting on periods (which can be incorrect, i.e. "Mr. Smith went to Washington"). You can open the first file for reading, open the new file for writing, and at ever period add a line break. – James Nov 20 '19 at 21:49
  • '\n' will start a new line. – Eli Nov 20 '19 at 21:50
  • @James you are right I did not consider this, however I am just a beginner programmer and in this case I don't need to exactly define a sentence (rather just split on breaks) – Hemal Nov 20 '19 at 21:51

3 Answers3

2

This can be done using the replace method for strings. https://www.geeksforgeeks.org/python-string-replace/

Basically, you just tell it what you are wanting to replace (in this case punctuation at the end of a sentence) and what to replace it with (a new line).

    string = 'First sentence. Second. Third.'
    print(string)

    string = string.replace(". ", ".\n")
    print(string)

Consider using replace for ? and ! as well.

As for writing to the result file, after you have the modified text in your string variable:

with open("output_file.txt", "w") as output_file:
    output_file.write(string)

The "w" parameter on open allows you to write to the file.

Branson Smith
  • 462
  • 2
  • 13
  • 1
    How can I add ? and ! to this? Just adding two more replace lines? – Hemal Nov 20 '19 at 21:53
  • 1
    That would be the simplest way, which is a good route for a brand new beginner! – Branson Smith Nov 20 '19 at 21:54
  • also how do I get string from the first file to the string to another? Could you show me how this works? – Hemal Nov 20 '19 at 21:54
  • i looked up the open() for python just wondering how to link the content from the first txt to another – Hemal Nov 20 '19 at 21:55
  • This post addresses that question: https://stackoverflow.com/questions/15343743/copying-from-one-text-file-to-another-using-python/32977473. Look at the top answer by @ATOzTOA. Basically, you'll need to open up an input file and an output file. Then use outputfile.write(string). I will add it to the bottom of my original code snippet. – Branson Smith Nov 20 '19 at 22:02
  • I added the code to my question, still not working. Could you check where did I make a mistake? – Hemal Nov 20 '19 at 22:26
  • 1
    Remove the top and bottom two lines. You are opening and closing the files using "with" already. – Branson Smith Nov 20 '19 at 23:08
0

You first need to define what is a sentence for you. On this case, a sentence is separeted by '.', so you just need to imput:

a = "First sentence. Second. Third." print(a.replace(". ", ".\n"))

But if you will consider others caracteres, like '!', '?' and ';', you will also put those options too.

All code:

f = open("demofile.txt", "r")
print(f.read())

a = f.read()
print(a.replace(". ", ".\n"))

f = open("demofile2.txt", "a")
f.write(a.replace(". ", ".\n"))
  • How can I although copy the string from the first file and then modify it like you suggested to another txt file? – Hemal Nov 20 '19 at 21:59
0
text = "First sentence. Second. Third."
text = text.split(". ")
for x in range(0,len(text)):
    if "." in text[x]:
        text[x] = text[x][:-1]
    print(text[x]+".")
Marcell
  • 1
  • 3