string.translate() with unicode data in python

后端 未结 5 491
情书的邮戳
情书的邮戳 2020-12-03 05:05

I have 3 API\'s that return json data to 3 dictionary variables. I am taking some of the values from the dictionary to process them. I read the specific values that I want t

5条回答
  •  忘掉有多难
    2020-12-03 05:48

    Python re module allows to use a function as a replacement argument, which should take a Match object and return a suitable replacement. We may use this function to build a custom character translation function:

    import re
    
    def mk_replacer(oldchars, newchars):
        """A function to build a replacement function"""
        mapping = dict(zip(oldchars, newchars))
        def replacer(match):
            """A replacement function to pass to re.sub()"""
            return mapping.get(match.group(0), "")
        return replacer
    

    An example. Match all lower-case letters ([a-z]), translate 'h' and 'i' to 'H' and 'I' respectively, delete other matches:

    >>> re.sub("[a-z]", mk_replacer("hi", "HI"), "hail")
    'HI'
    

    As you can see, it may be used with short (incomplete) replacement sets, and it may be used to delete some characters.

    A Unicode example:

    >>> re.sub("[\W]", mk_replacer(u'\u0435\u0438\u043f\u0440\u0442\u0432', u"EIPRTV"), u'\u043f\u0440\u0438\u0432\u0435\u0442')
    u'PRIVET'
    

提交回复
热议问题