I’m working with a function I made to split this sample line below to remove the standalone numerical values (123), however it’s also removing the trailing numbers which I need. I also can’t figure out how to remove the “0.0”
ABC/0.0/123/TT1/1TT//
cleaned_data = [] def split_lines(lines, delimiter, remove = '[0-9]+$'): for line in lines: tokens = line.split(delimiter) tokens = [re.sub(remove, "", token) for token in tokens] clean_list = list(filter(lambda e:e.strip(), tokens)) cleaned_data.append(clean_list) print(clean_list) split_lines(lines, "/")
What’s coming out now is below, notice the 0. and “TT” that’s missing the trailing 1.
[ABC], [0.], [TT], [1TT]
Advertisement
Answer
Try including the start of line anchor (^) as well.
cleaned_data = [] def split_lines(lines, delimiter, remove = '^[0-9.]+$'): for line in lines: tokens = line.split(delimiter) tokens = [re.sub(remove, "", token) for token in tokens] clean_list = list(filter(lambda e:e.strip(), tokens)) cleaned_data.append(clean_list) print(clean_list) split_lines(lines, "/")
I simply changed the default value of the remove
parameter to ‘^[0-9.]+$’ which only matches if the entire search string is numbers (or a period).