Another approach to parse numbers from complex strings

I would like to share another way to interpret numbers from complex strings for task 7/10 Cleaning and Preparing Data in Python.
In the original method, there are given some bad_chars = ["(",")",“c”,“C”,".",“s”,"’", " "] which we should replace in Date column in data set to get clean data. There are about 17 000 rows and we can not examine each row to know what kind of bad_chars that each cell may contain. So, that is why I decided to check each substring in a string is numeric or not, by using string built-in method isnumeric(), we need to delete non-numeric ones and not delete “-” dash symbol. The result is the same.

> test_data = ["1912", "1929", "1913-1923",
>                  "(1951)", "1994", "1934",
>                  "c. 1915", "1995", "c. 1912",
>                  "(1988)", "2002", "1957-1959",
>                  "c. 1955.", "c. 1970's", 
>                  "C. 1990-1999"]

Original solution: 

> bad_chars = ["(",")","c","C",".","s","'", " "]
> 
> def strip_characters(s):
>     for char in bad_chars:
>         s = s.replace(char, '')
>     return s

My solution:

> def strip_characters(s):
>   for char in s:
>     if not char.isnumeric() and char != "-":
>         s = s.replace(char, "")
>   return s
4 Likes