str.translate gives TypeError - Translate takes one argument (2 given), worked in Python 2

前端 未结 5 1968
春和景丽
春和景丽 2020-11-29 00:24

I have the following code

import nltk, os, json, csv, string, cPickle
from scipy.stats import scoreatpercentile

lmtzr = nltk.stem.wordnet.WordNetLemmatizer         


        
5条回答
  •  时光取名叫无心
    2020-11-29 01:27

    This is how translate works:

    yourstring.translate(str.maketrans(fromstr, tostr, deletestr))
    

    Replace the characters in fromstr with the character in the same position in tostr and delete all characters that are in deletestr. The fromstr and tostr can be empty strings and the deletestr parameter can be omitted.

    example:

    str="preetideepak12345aeiou"
    >>> str.translate(str.maketrans('abcde','12345','p'))
    

    output:

    'r55ti4551k1234515iou'
    

    here:

    a is translated to 1
    b is translated to 2
    c is translated to 3 and so on
    and p is deleted from string.
    

提交回复
热议问题