Add replace option to CleanText
This commit is contained in:
parent
b79215733c
commit
6138209a2f
1 changed files with 10 additions and 2 deletions
|
|
@ -170,16 +170,18 @@ class CleanText(Filter):
|
|||
string.
|
||||
Second, it replaces all symbols given in second argument.
|
||||
"""
|
||||
def __init__(self, selector, symbols='', **kwargs):
|
||||
def __init__(self, selector, symbols='', replace=[], **kwargs):
|
||||
super(CleanText, self).__init__(selector, **kwargs)
|
||||
self.symbols = symbols
|
||||
self.toreplace = replace
|
||||
|
||||
def filter(self, txt):
|
||||
if isinstance(txt, (tuple,list)):
|
||||
txt = ' '.join(map(self.clean, txt))
|
||||
|
||||
txt = self.clean(txt)
|
||||
return self.remove(txt, self.symbols)
|
||||
txt = self.remove(txt, self.symbols)
|
||||
return self.replace(txt, self.toreplace)
|
||||
|
||||
@classmethod
|
||||
def clean(cls, txt):
|
||||
|
|
@ -195,6 +197,12 @@ class CleanText(Filter):
|
|||
txt = txt.replace(symbol, '')
|
||||
return txt
|
||||
|
||||
@classmethod
|
||||
def replace(cls, txt, replace):
|
||||
for before, after in replace:
|
||||
txt = txt.replace(before, after)
|
||||
return txt
|
||||
|
||||
|
||||
class Lower(CleanText):
|
||||
def filter(self, txt):
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue