我可能在这里真的很蠢,但我无法弄清楚这个错误:
'str' object has no attribute 'punctuation'
这发生在这一行:
docLines[counter][counter2] = [(docLines[counter][counter2]).translate(None, string.punctuation)]
docLines[counter][counter2]
只是一个单词。
我出错的任何想法?
答案 0 :(得分:12)
您已为名为str
的变量分配了一个字符串(string
的实例)。重命名变量,问题就会消失。
要调试此项,请在违规行之前添加print repr(string)
,它将打印一个字符串实例。模块中不同位置的大量此类打印将帮助您发现名称string
停止引用string
模块的位置,并开始引用str
实例。