469,133 Members | 1,089 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,133 developers. It's quick & easy.

Finding all numbers in string and replacing with ''

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
3 28729
bvdet
2,851 Expert Mod 2GB
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
Bouzy
30
Thanks! That worked exactly as needed.
Nov 2 '08 #3
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.

Similar topics

3 posts views Thread by Cengiz Ulku | last post: by
reply views Thread by Glenn Fleishman | last post: by
6 posts views Thread by Matthias S. | last post: by
3 posts views Thread by GeRmIc | last post: by
reply views Thread by Lakhi | last post: by
5 posts views Thread by sg14 | last post: by
1 post views Thread by CARIGAR | last post: by
1 post views Thread by Mortomer39 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.