I want to remove numeric values without losing numbers that are part of alphanumeric words from a string.
String = "Jobid JD123 has been abended with code 346"
Result = "Jobid JD123 has been abended with code"
I am using the following code:
result = ''.join([i for i in String if not i.isdigit()])
which gives me the result as 'Jobid JD has been abended with code
'
Is there anyway we can remove the words that only contain digits, while retaining those that contain a mix of letters and digits?
CodePudding user response:
You can use regex to find runs of one or more digits \d
between two word boundaries \b
, and replace them with nothing.
>>> import re
>>> string = "Jobid JD123 has been abended with code 346"
>>> re.sub(r"\b\d \b", "", string).strip()
'Jobid JD123 has been abended with code'
Note that the regex doesn't get rid of the trailing space (between "code"
and the digits), so you need to strip()
the result of the re.sub()
.
CodePudding user response:
Use .isnumeric()
to remove any word that doesn't only contain numbers:
s = "Jobid JD123 has been abended with code 346"
result = ' '.join(c for c in s.split() if not c.isnumeric())
print(result)
This outputs:
Jobid JD123 has been abended with code
CodePudding user response:
split
the string into words and check if the entire word is numerical
Result = " ".join(word for word in String.split() if not word.isnumeric())
>>> Result
'Jobid JD123 has been abended with code'