1

text with broken line and dash:

to validate my solution was
I need to take the bench test
of the elaborated algorithm
only after the table test that
the program was implemen-
ted this strategy spared
development time

code:

def file_string():
    with open('speech.txt','r') as file:
        lines = file.read().split("\n")
     string = []
     for line in lines:
         line = line.replace('-\n','')
         string.append(line)
     return (' '.join(string))

print(file_string())

Correct output:

to validate my solution was I need to take the bench test of the elaborated algorithm only after the table test that the program was implemented this strategy spared development time

Exit from my code:

to validate my solution was I need to take the bench test of the elaborated algorithm only after the table test that the program was implemen- ted this strategy spared development time

The text was written in the text editor.

I need this help.

applying the code sujerido the exit remained:

to validate my solution was I need to take the bench test of the elaborated algorithm only after the table test that the program was implemen ted this strategy spared development time

only happens when I read the file written in a text editor, I need to create a list with these words to make a comparison.

2 Answers 2

1

This line

lines = file.read().split("\n")

removes the '\n' from your lines because it splits on them. Characters you split on are never part of the results.

So this line

line = line.replace('-\n','')

can not find anything to replace.

Use line = line.rstrip("-") instead, it will remove the '-' from the right end of your string if present.

You might benefit from reading/following How to debug small programs (#1) - to get some tips on how to debug your own program.

Edit:

  • You get a ' ' from your ' '-join()ing of split lines - you need to keep track on which lines ends on - and merge it with the following one. Its easier to simply do 2 replaces like this:

def file_string():
    with open('speech.txt','r') as file:
        lines = file.read()

    return lines.replace('-\n','').replace('\n', ' ') 

print(file_string())

to come to your wanted result. Uncomment the commented lines and remove the lines = """...""".

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

6 Comments

hello patrick, I modified the code but got a space between the word.(implemen ted).
@Bruno - you are right. I propose a different solution that first replaces '-\n' by nothing and then '\n' by ' ' to get to the desired output.
the problem occurs when I read from a text file, the word continues with the space, I do not know but what I do.
@Bruno the edited version still gives you wrong output?
Yes @patrick , I do not know but what I do, this happens when I read the text file edited in notpad.
|
1

This will do (updated)?

import re

def file_string():
    with open('speech.txt','r') as file:
        lines = file.read()
    lstr = re.sub(r'\-[\n]*',r'',lines)
    lstr = re.sub(r'[\n]+',r' ',lstr)
    return lstr

print(file_string())

2 Comments

The text was created in a text editor, there is some possibility that it is giving error for this reason
Friends many excuses, the mistake is that I was copying the text from a pdf and pasting it into the text editor. All codes are correct and working.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.