alvas
alvas

Reputation: 122148

How to use str.replace() with a dictionary of replacements? Python

Given a dictionary of replacements, where key = to be replaced and value = replacements, e.g.:

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

How do i perform a replace without iterating through the replacements.keys()?

How will the same operation be possible with regex, re.sub()?

I have been doing it this way:

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

Upvotes: 1

Views: 282

Answers (1)

Martijn Pieters
Martijn Pieters

Reputation: 1123570

You are looking for unicode.translate() instead. It takes a mapping of unicode ordinals (integer numbers) and the values should be ordinals too, or unicode strings, or None to signal to delete that character:

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

Demo:

>>> 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!'"

Upvotes: 5

Related Questions