Re:sub expected string or byte-like object error

Question:

class REReplacer(object):
   def __init__(self, pattern = R_patterns):
      self.pattern = [(re.compile(regex), repl) for (regex, repl) in patterns]

   def replace(self, text):
      s = text
      for (pattern, repl) in self.pattern:
         s = re.sub(pattern, repl, s)
      return s

I have got this code which replaces certain words with their replacements. When I call the method replace of class REplacer,

rep=REplacer()
rep.replace("I like oranges") 

it works perfectly fine with strings but gives an error with list or nested lists.

Error- (re.sub) expected string or bytes-like object.

Is there a way (except for converting the list to string) in order to make the function work of list of sentences? Thanks in advance.
Seems like predefined re.sub takes string as argument. Should I split the words of the list?

Asked By: Codingamethyst

||

Answers:

If text is a list (or some other iterable type), loop over it and perform the replacements, and return a list of results.

from collections.abc import Iterable

def replace(self, text):
    if isinstance(text, string):
        for (pattern, repl) in self.pattern:
            text = re.sub(pattern, repl, text)
        return text
    elif isinstance(text, Iterable):
        return [self.replace(i) for i in text]
    else:
        raise ValueError('text must be a string or iterable collection')
Answered By: Barmar
Categories: questions Tags: , , , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.