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()
3
  • 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. Commented Nov 20, 2019 at 21:49
  • '\n' will start a new line. Commented Nov 20, 2019 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) Commented Nov 20, 2019 at 21:51

3 Answers 3

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.

Sign up to request clarification or add additional context in comments.

7 Comments

How can I add ? and ! to this? Just adding two more replace lines?
That would be the simplest way, which is a good route for a brand new beginner!
also how do I get string from the first file to the string to another? Could you show me how this works?
i looked up the open() for python just wondering how to link the content from the first txt to another
Remove the top and bottom two lines. You are opening and closing the files using "with" already.
|
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]+".")

2 Comments

Please provide context explaining your answer to prevent removal via "low-quality" review. (End of Review).
add appropriate explanation along side the code to justify why this works.
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"))

1 Comment

How can I although copy the string from the first file and then modify it like you suggested to another txt file?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.