如何从python中的文本中提取只包含字母的单词?

2024-04-25 03:41:05 发布

您现在位置:Python中文网/ 问答频道 /正文

例如在以下文本中:

"We’d love t0 help 123you, but the real1ty is th@t n0t every question gets answered. To improve your chances, here are some tips:"

如何轻松提取只包含字母的单词:

^{pr2}$

我试过了

words = re.findall(r'^[a-zA-Z]+',str)
    for word in words:
print word

其中str是文本。这做了一些工作,但我需要做些调整。在

对正则表达式有什么想法吗?在


Tags: the文本ishelpwordbutwewords
1条回答
网友
1楼 · 发布于 2024-04-25 03:41:05

你可以使用列表理解。在

s = "We’d love t0 help 123you, but the real1ty is th@t n0t every question gets answered. To improve your chances, here are some tips:"
print [i for i in s.split() if i.isalpha()]
  • s.split()将根据空格拆分输入。在
  • 只需迭代返回的项并考虑那些完全包含字母表的项。在

相关问题 更多 >