I am performing the following operations on lists of words. I read lines in from a Project Gutenberg text file, split each line on spaces, perform general punctuation substituti
What you really need to properly replace starting and ending '
is regex.
To match them you should use:
^' for starting ' (opensingle),'$ for ending ' (closesingle).Unfortunately, replace method does not support regexes,
so you should use re.sub instead.
Below you have an example program, printing your desired output (in Python 3):
import re
str = "don't 'George ma'am end.' didn't.' 'Won't"
words = str.split(" ")
for word in words:
word = re.sub(r"^'", '\n', word)
word = re.sub(r"'$", '\n', word)
word = word.replace('.', '\n')
word = word.replace(',', '\n')
print(word)