Python Replace Single Quotes Except Apostrophes

前端 未结 3 1559
没有蜡笔的小新
没有蜡笔的小新 2021-01-23 02:11

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

3条回答
  •  谎友^
    谎友^ (楼主)
    2021-01-23 02:54

    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)
    

提交回复
热议问题