【发布时间】:2016-09-22 13:55:32
【问题描述】:
这是我目前所拥有的:
import re
def read_file(file):
words = []
for line in file:
for word in line.split():
words.append(re.sub("[^a-z]", "", word.lower()))
就目前而言,这会将“can't”读作“cant”,将“co-ordinate”读作“coordinate”。我想读单词,以便允许这两个标点符号。如何修改我的代码来做到这一点?
【问题讨论】:
-
试试这个
re.sub(r"[^a-z\-']", "", word.lower()) -
@ritesht93 如果将连字符放在开头或结尾,则无需转义。
标签: python regex list file sanitization