I’ve got a pretty good working snippit of code, but I was wondering if anyone has any better suggestions on how to do this:
val = ''.join([c for c in val if c in '1234567890.'])
What would you do?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
You can use a regular expression (using the
remodule) to accomplish the same thing. The example below matches runs of[^\d.](any character that’s not a decimal digit or a period) and replaces them with the empty string. Note that if the pattern is compiled with theUNICODEflag the resulting string could still include non-ASCII numbers. Also, the result after removing “non-numeric” characters is not necessarily a valid number.