I have a list of words and am trying to replace all the numbers in my list with whitespace. - for word in words:
-
numbers = re.search('[0-9]+', word)
-
word = clearup(word)
-
if word in dictionary:
-
pass
-
else:
-
print word
- 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?
3 29141 bvdet 2,851
Recognized Expert Moderator Specialist
Try this: - 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.punctuation+string.digits)
-
Output: - >>> this is a test string with embedded punctuation and numbers
Thanks! That worked exactly as needed.
Or if you use python 2.6 and s is not a unicode string: -
s.translate(None, string.punctuation+string.digits).lower()
-
which is approx. 12 times faster than a regex.
Sign in to post your reply or Sign up for a free account.
Similar topics |
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...
|
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...
|
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...
|
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...
|
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 ...
| |
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...
|
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...
|
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...
|
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...
|
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,...
|
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...
| |
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...
|
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...
|
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...
|
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,...
|
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...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
| |
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 ...
| |