【发布时间】:2013-09-23 14:14:30
【问题描述】:
我将单词定义为一系列字符(从 a 到 Z),其中可能还包含撇号。我希望将一个句子拆分成单词,去掉单词中的撇号。
我目前正在执行以下操作以从一段文本中获取单词。
import re
text = "Don't ' thread \r\n on \nme ''\n "
words_iter = re.finditer(r'(\w|\')+', text)
words = (word.group(0).lower() for word in words_iter)
for i in words:
print(i)
这给了我:
don't
'
thread
on
me
''
但我不想要的是:
dont
thread
on
me
如何更改我的代码以实现此目的?
请注意,我的输出中没有'。
我也希望words 成为生成器。
【问题讨论】:
-
你不是快到了,只需在你的
for循环中添加一个i = i.replace("'", ""),然后如果它不为空则生成字符串? -
您要解析多少输入?
-
@Tritium21 我正在构建一个语料库,所以我正在处理不同长度的文本文件。
标签: python regex python-3.x