Im using a API that returns the sentiment of a given sentence. I have a file filed with text, but I need to format the file such that each sentence is in a new line
so for example this is the text in the file.
"Do so written as raising parlors spirits mr elderly.Made late in of high left hold. Carried females of up highest calling. Limits marked led silent dining her she far. Sir but elegance marriage dwelling likewise position old pleasure men. Dissimilar themselves simplicity no of contrasted as. Delay great day hours men. Stuff front to do allow to asked he."
I want to format it such that it looks like this:
Do so written as raising parlors spirits mr elderly.
Made late in of high left hold. Carried females of up highest calling.
Limits marked led silent dining her she far.
Sir but elegance marriage dwelling likewise position old pleasure men.
Dissimilar themselves simplicity no of contrasted as.
Delay great day hours men. Stuff front to do allow to asked he.
And I can't do this manually because the final thing will have a large amount of text that will be formatted.
CodePudding user response:
See if it helps:
list_s = s.split(".")
write_file = open("write.txt","w")
for i in range(len(list_s)-1):
print(list_s[i].strip() ".",file=write_file)
write_file.close()
It splits the text based on .
,and writes each part to write_file
. The .strip()
will remove any leading or trailing white spaces in that line. If your text/paragraph has floating point numbers inside (12.24), this code won't work though. In that case, you may need some advanced processing.
CodePudding user response:
I think that is will be
C=0
for j in open("file").read():
open("otherfile.txt","w").write(str(j.split(".")[C]) "\n")