给定替换字典,其中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])

最佳答案

您正在寻找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!'"

10-08 05:38