Python_关键词替换输出

2017-06-22  本文已影响98人  古佛青灯度流年
# coding=utf-8
#!/usr/bin/python
import re
import six


class Xlator(dict):
    def _make_regex(self):
        return re.compile("|".join(map(re.escape, six.iterkeys(self))))

    def __call__(self, match):
        return self[match.group(0)]

    def xlat(self, text):
        return self._make_regex().sub(self, text)


text = "Larry Wall is the creator of Perl"
adict = {
    "Larry Wall": "Guido van Rossum",
    "creator": "Benevolent Dictator for Life",
    "Perl": "Python",
}
xlat = Xlator(adict)
print xlat.xlat(text)

Guido van Rossum is the Benevolent Dictator for Life of Python

@阴天-这几天说是有大暴雨

上一篇 下一篇

猜你喜欢

热点阅读