Reputation: 133
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.
Upvotes: 1
Views: 7156
Reputation: 411
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())
Upvotes: 1
Reputation: 51623
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:
' '-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 = """..."""
.
Upvotes: 1