-4
我想用re模块从文本中提取所有标点符号。我怎样才能做到这一点?如何在Python中使用Re模块提取所有标点符号?
我想用re模块从文本中提取所有标点符号。我怎样才能做到这一点?如何在Python中使用Re模块提取所有标点符号?
>>> text = "[email protected]#$%^&*()"
>>> from string import punctuation
>>> for p in punctuation:
... if p in text:
... print p
...
它会打印所有来自文本的标点字符。
!
#
$
%
&
(
)
*
@
^
OR
>>> text = "[email protected]#$%^&*()"
>>> [char for char in punctuation if char in text]
['!', '#', '$', '%', '&', '(', ')', '*', '@', '^']
我不知道如何与re
模块做到这一点,但您可以使用列表理解:
from string import punctuation
old_string = "This, by the way, has some punctuation!"
new_string = "".join(char for char in old_string if char in punctuation)
print(new_string)
#,,!
采用进口串; string.punctuation –
所以我做了什么知道的是: 进口串 P = [文字x.punctuation为X] 但unfortunaly不起作用。 –