473,493 Members | 2,254 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Finding all numbers in string and replacing with ''

30 New Member
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 29141
bvdet
2,851 Recognized Expert Moderator Specialist
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 New Member
Thanks! That worked exactly as needed.
Nov 2 '08 #3
markus314
2 New Member
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

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

Similar topics

3
2394
by: Cengiz Ulku | last post by:
Hi, How can look for only the numbers in a RTB control? I know for instance how to search for a string by using: : Private Sub Command1_Click() Dim FoundPos As Integer Dim FoundLine As...
0
2056
by: Glenn Fleishman | last post by:
I'm trying to perform a search that I can't find any information about in MySQL's manuals, discussion groups, or on the Web. I'm used to using BerkeleyDB's Btree, so I can browse to find the...
6
3795
by: Matthias S. | last post by:
Hi, I'm pretty sure this is as easy as it gets, but I couldn't find anything in the documentation. I'd like to change the string representation of an int so that the numbers get padded with...
3
1873
by: GeRmIc | last post by:
How do you find if the returned XML string does not contain any record/elements? The XML string i receive just has the structure if there are not records, How do I check for this? <?xml...
0
1405
by: Lakhi | last post by:
hi frnds, I need small help in String replacement I need to replace the text using replceAll() with Case-Insensitve . Is there any regular expression for this? i have this expression ...
2
3746
by: Spooner | last post by:
I have code that reads in a paragraph of words from a .txt file. How would I go about finding a string that a user inputs into the program? I read in the text as a char, so perhaps I should change...
1
1460
by: shaahul | last post by:
1. I would like to parse a C file and get the list of functions in that file. 2. And then search for a string of the pattern "funcName(var)" in each of those functions. Is it possible to do such a...
2
1432
by: pnsreee | last post by:
Hi All, I have the following strings seperated by space . I have to grep for Naveen having two values(Sha or See) in the string. 1)Naveen Sha reswww 2)Naveen See rex-www...
5
2305
by: sg14 | last post by:
I'm having a little trouble finding multiple occurrences of a string in a file. I have a txt file and I have to find e-mail addresses out of it. I open the file with ifstream, and use getline to...
0
7119
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
6989
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7195
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
6873
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7367
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5453
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
4889
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
3078
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1400
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.