By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,587 Members | 1,702 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,587 IT Pros & Developers. It's quick & easy.

Finding all numbers in string and replacing with ''

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

Expand|Select|Wrap|Line Numbers
  1. for word in words:
  2.       numbers = re.search('[0-9]+', word)
  3.       word = clearup(word)
  4.       if word in dictionary:
  5.          pass
  6.       else:
  7.          print word     
Expand|Select|Wrap|Line Numbers
  1. def clearup(tor):
  2.  
  3.    numbers = re.search('[0-9]+', tor)
  4.    return tor.replace('%s' % numbers, '')\
  5.    .replace('.', '')\
  6.    .replace(',', '')\
  7.    .replace(',', '')\
  8.    .replace(':', '')\
  9.    .replace('!', '')\
  10.    .replace('@', '')\
  11.    .replace('#', '')\
  12.    .replace('$', '')\
  13.    .replace('%', '')\
  14.    .replace('(', '')\
  15.    .replace(')', '')\
  16.    .replace('?', '')\
  17.    .replace('-', ' ')\
  18.    .replace(';', '')\
  19.    .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?
Nov 2 '08 #1
Share this Question
Share on Google+
3 Replies


bvdet
Expert Mod 2.5K+
P: 2,851
Try this:
Expand|Select|Wrap|Line Numbers
  1. import re, string
  2.  
  3. def clearup(s, chars):
  4.     return re.sub('[%s]' % chars, '', s).lower()
  5.  
  6. s = 'This is %a t1e22st !st4ring6 w.it6h 87embed766ded punct,:ua-tion and nu=mbe]rS6.'
  7.  
  8. print clearup(s, string.punctuation+string.digits)
  9.  
Output:
Expand|Select|Wrap|Line Numbers
  1. >>> this is a test string with embedded punctuation and numbers
Nov 2 '08 #2

P: 30
Thanks! That worked exactly as needed.
Nov 2 '08 #3

P: 2
Or if you use python 2.6 and s is not a unicode string:

Expand|Select|Wrap|Line Numbers
  1. s.translate(None, string.punctuation+string.digits).lower()
  2.  
which is approx. 12 times faster than a regex.
Nov 5 '08 #4

Post your reply

Sign in to post your reply or Sign up for a free account.