2

I have a very noisy string input and I am trying to clean it..

So, a part of the noisy string can be something like:

"big $price chair, 5x10"

Now removing symbols and other stuff are done! But I also want to remove

  5x10

for this I did this:

 def remove_numerics(self,string):
    return ' '.join([term for term in string.split() if not term[0].isdigit()])

Which solved this case

but if my string is:

    "big $price chair, x10"

Then it fails? what is a good pythonic way to solve this case also. many thanks.

1
  • So you want to remove all "words" of the form NUMBERSxNUMBERS? Commented May 23, 2012 at 17:43

2 Answers 2

5
re.sub(r'\b[\dx]+\b', '', "big $price chair, 5x10")
Sign up to request clarification or add additional context in comments.

Comments

4
import re
new_string = re.sub(r', \d*x\d+', '', old_string)

2 Comments

+1. Minor nitpick: you should never name variables after python builtins. Change str to something else.
@JoelCornett thanks... been writing C# all week... switched it the wrong way >.<

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.