1. Gregory Petukhov
  2. grab


Gregory Petukhov  committed 8cf1f59

Add ignore_chars option to grab.tools.text.find_number and to grab.item.field.IntegerField

  • Participants
  • Parent commits 4fd9be0
  • Branches default

Comments (0)

Files changed (2)

File grab/item/field.py

View file
 class IntegerField(Field):
     def __init__(self, *args, **kwargs):
         self.ignore_spaces = kwargs.get('ignore_spaces', False)
+        self.ignore_chars = kwargs.get('ignore_chars', None)
         super(IntegerField, self).__init__(*args, **kwargs)
         if self.empty_default is not NULL:
             if value == "":
                 return self.empty_default
-        return find_number(self.process(value), ignore_spaces=self.ignore_spaces)
+        return find_number(self.process(value), ignore_spaces=self.ignore_spaces,
+                           ignore_chars=self.ignore_chars)
 class StringField(Field):

File grab/tools/text.py

View file
 RE_SPACE = re.compile(r'\s+', re.U)
 BOM_TOKEN = '\xef\xbb\xbf'
-def find_number(text, ignore_spaces=False, make_int=True):
+def find_number(text, ignore_spaces=False, make_int=True,
+                ignore_chars=None):
     Find the number in the `text`.
     :raises: :class:`DataNotFound` if number was not found.
+    if ignore_chars:
+        for char in ignore_chars:
+            text = text.replace(char, '')
     if ignore_spaces:
         match = RE_NUMBER_WITH_SPACES.search(text)