【发布时间】:2016-12-19 15:02:18
【问题描述】:
我正在尝试从我的文本文件中删除标点符号列表,但我只有一个与连字符分隔的单词的问题。例如,如果我有“post-trauma”这个词,我会得到“posttrama”,相反,我想得到“post”“trauma”。
我的代码是:
punct=['!', '#', '"', '%', '$', '&', ')', '(', '+', '*', '-']
with open(myFile, "r") as f:
text= f.read()
remove = '|'.join(REMOVE_LIST) #list of word to remove
regex = re.compile(r'('+remove+r')', flags=re.IGNORECASE)
out = regex.sub("", text)
delta= " ".join(out.split())
txt = "".join(c for c in delta if c not in punct )
有办法解决吗?
【问题讨论】:
-
你应该使用正则表达式,你不希望 [a-zA-Z].*-[a-z] 被删除
-
扔掉这个,你试过=regex.sub(" ",text)吗?
标签: python list punctuation