I got the following list called "phonenumbers". I struggle to remove the elements which contain '\n\t\t\t' and '\n\t\t\t\t'.
I tried "try and except"-methode and remove('\n\t\t\t\t') but couldnt make it work.
Any tips?
['(02271) 6 79', ' 70', '\n\t\t\t', '(02271) 6 79', '\n\t\t\t\t', ' 70', '\n\t\t\t', '\n\t\t\t', '(02181) 27 0', '\n\t\t\t\t', '3-0', '\n\t\t\t', '\n\t\t\t', '(02181) 27 0', '\n\t\t\t\t', '3-0', '\n\t\t\t', '\n\t\t\t', '(02161) 24 19', '\n\t\t\t\t', ' 40', '\n\t\t\t', '\n\t\t\t', '(02161) 24 19', '\n\t\t\t\t', ' 40', '\n\t\t\t', '\n\t\t\t', '(02131) 66 67', '\n\t\t\t\t', ' 10', '\n\t\t\t', '\n\t\t\t', '(02131) 66 67', '\n\t\t\t\t', ' 10', '\n\t\t\t', '\n\t\t\t', '(02103) 39 00', '\n\t\t\t\t', ' 93', '\n\t\t\t', '\n\t\t\t', '(02103) 39 00', '\n\t\t\t\t', ' 93', '\n\t\t\t', '\n\t\t\t', '(02173) 2 04 7', '\n\t\t\t\t', '3-0', '\n\t\t\t', '\n\t\t\t', '(02173) 2 04 7', '\n\t\t\t\t', '3-0', '\n\t\t\t', '\n\t\t\t', '(02235) 9 23 04', '\n\t\t\t\t', ' 30', '\n\t\t\t', '\n\t\t\t', '(02235) 9 23 04', '\n\t\t\t\t', ' 30', '\n\t\t\t', '\n\t\t\t', '\n\t\t\t\t', '(0221) 3 46 79 40', '\n\t\t\t', '\n\t\t\t', '\n\t\t\t\t', '(0221) 3 46 79 40', '\n\t\t\t', '\n\t\t\t', '(02232) 4 23', '\n\t\t\t\t', ' 05', '\n\t\t\t', '\n\t\t\t', '(02232) 4 23', '\n\t\t\t\t', ' 05', '\n\t\t\t', '\n\t\t\t', '(0157) 86 85 74', '\n\t\t\t\t', ' 43', '\n\t\t\t', '\n\t\t\t', '(0157) 86 85 74', '\n\t\t\t\t', ' 43', '\n\t\t\t', '\n\t\t\t', '(02181) 2 78 11', '\n\t\t\t\t', ' 47', '\n\t\t\t', '\n\t\t\t', '(02181) 2 78 11', '\n\t\t\t\t', ' 47', '\n\t\t\t', '\n\t\t\t', '(02181) 47 49 0', '\n\t\t\t\t', '0-0', '\n\t\t\t', '\n\t\t\t', '(02181) 47 49 0', '\n\t\t\t\t', '0-0', '\n\t\t\t', '\n\t\t\t', '(02202) 1 88', '\n\t\t\t\t', ' 60', '\n\t\t\t', '\n\t\t\t', '(02202) 1 88', '\n\t\t\t\t', ' 60', '\n\t\t\t', '\n\t\t\t', '(0211) 23 80', '\n\t\t\t\t', ' 70', '\n\t\t\t', '\n\t\t\t', '(0211) 23 80', '\n\t\t\t\t', ' 70', '\n\t\t\t', '\n\t\t\t', '(02235) 9 23 0', '\n\t\t\t\t', '4-0', '\n\t\t\t', '\n\t\t\t', '(02235) 9 23 0', '\n\t\t\t\t', '4-0', '\n\t\t\t']
You could go for a simple expression like
^\s+$
In Python
:
import relst = ['(02271) 6 79', ' 70', '\n\t\t\t', '(02271) 6 79', '\n\t\t\t\t', ' 70', '\n\t\t\t', '\n\t\t\t', '(02181) 27 0', '\n\t\t\t\t', '3-0', '\n\t\t\t', '\n\t\t\t', '(02181) 27 0', '\n\t\t\t\t', '3-0', '\n\t\t\t', '\n\t\t\t', '(02161) 24 19', '\n\t\t\t\t', ' 40', '\n\t\t\t', '\n\t\t\t', '(02161) 24 19', '\n\t\t\t\t', ' 40', '\n\t\t\t', '\n\t\t\t', '(02131) 66 67', '\n\t\t\t\t', ' 10', '\n\t\t\t', '\n\t\t\t', '(02131) 66 67', '\n\t\t\t\t', ' 10', '\n\t\t\t', '\n\t\t\t', '(02103) 39 00', '\n\t\t\t\t', ' 93', '\n\t\t\t', '\n\t\t\t', '(02103) 39 00', '\n\t\t\t\t', ' 93', '\n\t\t\t', '\n\t\t\t', '(02173) 2 04 7', '\n\t\t\t\t', '3-0', '\n\t\t\t', '\n\t\t\t', '(02173) 2 04 7', '\n\t\t\t\t', '3-0', '\n\t\t\t', '\n\t\t\t', '(02235) 9 23 04', '\n\t\t\t\t', ' 30', '\n\t\t\t', '\n\t\t\t', '(02235) 9 23 04', '\n\t\t\t\t', ' 30', '\n\t\t\t', '\n\t\t\t', '\n\t\t\t\t', '(0221) 3 46 79 40', '\n\t\t\t', '\n\t\t\t', '\n\t\t\t\t', '(0221) 3 46 79 40', '\n\t\t\t', '\n\t\t\t', '(02232) 4 23', '\n\t\t\t\t', ' 05', '\n\t\t\t', '\n\t\t\t', '(02232) 4 23', '\n\t\t\t\t', ' 05', '\n\t\t\t', '\n\t\t\t', '(0157) 86 85 74', '\n\t\t\t\t', ' 43', '\n\t\t\t', '\n\t\t\t', '(0157) 86 85 74', '\n\t\t\t\t', ' 43', '\n\t\t\t', '\n\t\t\t', '(02181) 2 78 11', '\n\t\t\t\t', ' 47', '\n\t\t\t', '\n\t\t\t', '(02181) 2 78 11', '\n\t\t\t\t', ' 47', '\n\t\t\t', '\n\t\t\t', '(02181) 47 49 0', '\n\t\t\t\t', '0-0', '\n\t\t\t', '\n\t\t\t', '(02181) 47 49 0', '\n\t\t\t\t', '0-0', '\n\t\t\t', '\n\t\t\t', '(02202) 1 88', '\n\t\t\t\t', ' 60', '\n\t\t\t', '\n\t\t\t', '(02202) 1 88', '\n\t\t\t\t', ' 60', '\n\t\t\t', '\n\t\t\t', '(0211) 23 80', '\n\t\t\t\t', ' 70', '\n\t\t\t', '\n\t\t\t', '(0211) 23 80', '\n\t\t\t\t', ' 70', '\n\t\t\t', '\n\t\t\t', '(02235) 9 23 0', '\n\t\t\t\t', '4-0', '\n\t\t\t', '\n\t\t\t', '(02235) 9 23 0', '\n\t\t\t\t', '4-0', '\n\t\t\t']rx = re.compile(r'^\s+$')lst = [item.strip() for item in lst if not rx.match(item)]
print(lst)
This yields and strips all numbers that are not only whitespaces from start to end:
['(02271) 6 79', '70', '(02271) 6 79', '70', '(02181) 27 0', '3-0', '(02181) 27 0', '3-0', '(02161) 24 19', '40', '(02161) 24 19', '40', '(02131) 66 67', '10', '(02131) 66 67', '10', '(02103) 39 00', '93', '(02103) 39 00', '93', '(02173) 2 04 7', '3-0', '(02173) 2 04 7', '3-0', '(02235) 9 23 04', '30', '(02235) 9 23 04', '30', '(0221) 3 46 79 40', '(0221) 3 46 79 40', '(02232) 4 23', '05', '(02232) 4 23', '05', '(0157) 86 85 74', '43', '(0157) 86 85 74', '43', '(02181) 2 78 11', '47', '(02181) 2 78 11', '47', '(02181) 47 49 0', '0-0', '(02181) 47 49 0', '0-0', '(02202) 1 88', '60', '(02202) 1 88', '60', '(0211) 23 80', '70', '(0211) 23 80', '70', '(02235) 9 23 0', '4-0', '(02235) 9 23 0', '4-0']
As @dawg points out, a regular expression is not really needed, actually:
lst = [number for item in lst for number in [item.strip()] if number]