使用 string.punctuation python 时出错

Pri*_*jan 0 python string punctuation python-3.x

我尝试在 python 中使用 string.punctuation 函数。我在 python 3.5 版本中这样做。这是我的以下代码:

import string
def check(text):
    x=set(text.punctuation())
Run Code Online (Sandbox Code Playgroud)

我收到以下错误

'str' object has no attribute 'punctuation'
Run Code Online (Sandbox Code Playgroud)

请帮助正确使用 this 的方法。我什至尝试使用 ispunct() 功能。

我尝试解决这个问题,如下面的链接所示,但无法弄清楚。

在 Python 中从字符串中去除标点符号的最佳方法

Kas*_*mvd 6

punctuationstring模块的方法而不是字符串对象属性。

>>> import string
>>> 
>>> string.punctuation
'!"#$%&\'()*+,-./:;<=>?@[\\]^_`{|}~'
Run Code Online (Sandbox Code Playgroud)

如果您想punctuation从文本中获取 ,您可以在 内使用生成器表达式set

set(i for i in text if i in string.punctuation)
Run Code Online (Sandbox Code Playgroud)