Finding all numbers in string and replacing with ''

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Bouzy
    New Member
    • Jun 2007
    • 30

    Finding all numbers in string and replacing with ''

    I have a list of words and am trying to replace all the numbers in my list with whitespace.

    Code:
    for word in words:
          numbers = re.search('[0-9]+', word)
          word = clearup(word)
          if word in dictionary:
             pass
          else:
             print word
    Code:
    def clearup(tor):
    
       numbers = re.search('[0-9]+', tor)
       return tor.replace('%s' % numbers, '')\
       .replace('.', '')\
       .replace(',', '')\
       .replace(',', '')\
       .replace(':', '')\
       .replace('!', '')\
       .replace('@', '')\
       .replace('#', '')\
       .replace('$', '')\
       .replace('%', '')\
       .replace('(', '')\
       .replace(')', '')\
       .replace('?', '')\
       .replace('-', ' ')\
       .replace(';', '')\
       .lower()
    I am not getting any errors, but when the words are printed, all of the numbers in in list words are printed. So it doesn't give me any errors but it doesn't work. What would I need to do to accomplish my task?
  • bvdet
    Recognized Expert Specialist
    • Oct 2006
    • 2851

    #2
    Try this:
    [code=Python]import re, string

    def clearup(s, chars):
    return re.sub('[%s]' % chars, '', s).lower()

    s = 'This is %a t1e22st !st4ring6 w.it6h 87embed766ded punct,:ua-tion and nu=mbe]rS6.'

    print clearup(s, string.punctuat ion+string.digi ts)
    [/code]
    Output:
    [code=Python]>>> this is a test string with embedded punctuation and numbers[/code]

    Comment

    • Bouzy
      New Member
      • Jun 2007
      • 30

      #3
      Thanks! That worked exactly as needed.

      Comment

      • markus314
        New Member
        • Oct 2008
        • 2

        #4
        Or if you use python 2.6 and s is not a unicode string:

        Code:
        s.translate(None, string.punctuation+string.digits).lower()
        which is approx. 12 times faster than a regex.

        Comment

        Working...