'str'对象没有属性'标点符号'

时间:2012-11-02 15:07:30

标签: python string

我可能在这里真的很蠢,但我无法弄清楚这个错误:

'str' object has no attribute 'punctuation'

这发生在这一行:

docLines[counter][counter2] = [(docLines[counter][counter2]).translate(None, string.punctuation)]

docLines[counter][counter2]只是一个单词。

我出错的任何想法?

1 个答案:

答案 0 :(得分:12)

您已为名为str的变量分配了一个字符串(string的实例)。重命名变量,问题就会消失。

要调试此项,请在违规行之前添加print repr(string),它将打印一个字符串实例。模块中不同位置的大量此类打印将帮助您发现名称string停止引用string模块的位置,并开始引用str实例。