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)

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM