1

给定一个替换字典,其中key = to be replacedvalue = replacements,例如:

replacements = {u'\u2014':'-', u'\u2019':"'", u'\u2018':"'", u'\u201d':'"', u'\u201c':'"'}

如何在不迭代的情况下执行替换replacements.keys()

正则表达式如何实现相同的操作re.sub()

我一直这样做:

for r in replacements:
  sentence = sentence.replace(r,replacements[r])
4

1 回答 1

5

你正在寻找unicode.translate()。它需要一个 unicode序数(整数)的映射,并且值也应该是序数,或 unicode 字符串,或None发出删除该字符的信号:

replacements = {ord(k): ord(v) for k, v in replacements.iteritems()}
sentence = sentence.translate(replacements)

演示:

>>> replacements = {ord(k): ord(v) for k, v in replacements.iteritems()}
>>> replacements
{8216: 39, 8217: 39, 8212: 45, 8221: 34, 8220: 34}
>>> u'\u2019Hello world! \u2014 You rock!\u2018'.translate(replacements)
u"'Hello world! - You rock!'"
于 2013-07-22T09:17:18.890 回答