给出替换词典,key = to be replaced
和value = 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])
答案 0 :(得分: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!'"