📌  相关文章
📜  Python|从给定的字符串中提取单词

📅  最后修改于: 2022-05-13 01:55:29.172000             🧑  作者: Mango

Python|从给定的字符串中提取单词

我们有时会遇到需要获取字符串中存在的所有单词的情况,这可能是使用本机方法完成的一项繁琐的任务。因此,使用速记来执行此任务总是很有用的。此外,本文还包括必须忽略标点符号的情况。
方法 #1:使用 split()
使用 split函数,我们可以将字符串拆分为单词列表,如果希望完成此特定任务,这是最通用和推荐的方法。但缺点是在字符串包含标点符号的情况下它会失败。

Python3
# Python3 code to demonstrate
# to extract words from string
# using split()
 
# initializing string 
test_string = "Geeksforgeeks is best Computer Science Portal"
 
# printing original string
print ("The original string is : " +  test_string)
 
# using split()
# to extract words from string
res = test_string.split()
 
# printing result
print ("The list of words is : " +  str(res))


Python3
# Python3 code to demonstrate
# to extract words from string
# using regex( findall() )
import re
 
# initializing string 
test_string = "Geeksforgeeks,    is best @# Computer Science Portal.!!!"
 
# printing original string
print ("The original string is : " +  test_string)
 
# using regex( findall() )
# to extract words from string
res = re.findall(r'\w+', test_string)
 
# printing result
print ("The list of words is : " +  str(res))


Python3
# Python3 code to demonstrate
# to extract words from string
# using regex() + string.punctuation
import re
import string
 
# initializing string 
test_string = "Geeksforgeeks,    is best @# Computer Science Portal.!!!"
 
# printing original string
print ("The original string is : " +  test_string)
 
# using regex() + string.punctuation
# to extract words from string
res = re.sub('['+string.punctuation+']', '', test_string).split()
 
# printing result
print ("The list of words is : " +  str(res))


输出:
原始字符串是:Geeksforgeeks 是最好的计算机科学门户
单词列表是:['Geeksforgeeks', 'is', 'best', 'Computer', 'Science', 'Portal']


方法#2:使用正则表达式(findall())
在包含所有特殊字符和标点符号的情况下,如上所述,使用拆分在字符串中查找单词的传统方法可能会失败,因此需要正则表达式来执行此任务。 findall函数在过滤字符串并提取忽略标点符号的单词后返回列表。

Python3

# Python3 code to demonstrate
# to extract words from string
# using regex( findall() )
import re
 
# initializing string 
test_string = "Geeksforgeeks,    is best @# Computer Science Portal.!!!"
 
# printing original string
print ("The original string is : " +  test_string)
 
# using regex( findall() )
# to extract words from string
res = re.findall(r'\w+', test_string)
 
# printing result
print ("The list of words is : " +  str(res))
输出:
原文是: 字符串 ,最好是@#计算机科学门户。!!!
单词列表是:['Geeksforgeeks', 'is', 'best', 'Computer', 'Science', 'Portal']


方法#3:使用 regex() + 字符串.punctuation
该方法也使用了正则表达式,但使用了获取所有标点的字符串函数,用于忽略所有标点符号,得到过滤后的结果字符串。

Python3

# Python3 code to demonstrate
# to extract words from string
# using regex() + string.punctuation
import re
import string
 
# initializing string 
test_string = "Geeksforgeeks,    is best @# Computer Science Portal.!!!"
 
# printing original string
print ("The original string is : " +  test_string)
 
# using regex() + string.punctuation
# to extract words from string
res = re.sub('['+string.punctuation+']', '', test_string).split()
 
# printing result
print ("The list of words is : " +  str(res))
输出:
原文是: 字符串 ,最好是@#计算机科学门户。!!!
单词列表是:['Geeksforgeeks', 'is', 'best', 'Computer', 'Science', 'Portal']