Reputation: 67
Whish to change the document below into a txt file (with text) and enter the file changing it so that every sentence is on it's own line. I tried with open() and get an error. Anyone know how to handle this one?
When I write the following I get an IndexError: string index out of range
document = "Hello World. Goodbye World"
def sentence_separator(document):
pos = 0
for char in document:
if char[pos] == ".":
char[pos] = "\n"
pos += 1
print(sentence_separator(document))
Desired output is each sentence on its separate lines:
Hello World
Goodby World
Upvotes: 1
Views: 1132
Reputation: 196
You use str.replace to replace '.' by line returns:
def sentence_separator(document):
return document.replace('.', '\n')
Upvotes: 0
Reputation: 486
There is an easy way to do this:
document = "Hello World. Goodbye World"
def sentence_separator(document):
sentences = document.split(". ")
return "\n".join(sentences)
print(sentence_separator(document))
NOTE: The reason why you got this error IndexError: string index out of range is because with for in loop working with string, each incrementation takes one character for that string. So at this line:
if char[pos] == ".":
Imagine if 1 is the value of pos
variable: it will try to find the second character of one character (sounds bad, it'snt?). Consequently, you get IndexError: string index out of range
Upvotes: 0
Reputation: 12918
You can use the split()
method with an argument to specify which character to split on, then print each element in the resulting array:
sentences = document.split('.')
print([s.strip() for s in sentences])
And the s.strip()
gets rid of any extra spaces around the period.
Upvotes: 1
Reputation: 111
You can use the replace function:
print(document.replace('.','\n')
Upvotes: 0
Reputation: 164673
You can just use str.join
:
document = "Hello World. Goodbye World"
print('\n'.join(document.split('. ')))
Hello World
Goodbye World
Upvotes: 1