TypeError: translate() takes exactly one argument (2 given)

Posted yangruicvpr

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了TypeError: translate() takes exactly one argument (2 given)相关的知识,希望对你有一定的参考价值。

python3.6下使用translate(None, string.punctuation)去除句子中的标点符号,报错:TypeError: translate() takes exactly one argument (2 given)

原因是python版本的问题,python2下该语句正常执行,python3中translate的参数只有一个,正确做法:

sentence=‘The girl is a painter, and her sisiter is a dancer.‘

trans=str.maketrans({key: None for key in string.punctuation})#建立转换关系

result=sentence.translate(trans)

 

以上是关于TypeError: translate() takes exactly one argument (2 given)的主要内容,如果未能解决你的问题,请参考以下文章