I am trying to work out a simple function to split lists.
Here is one example list that i am working on
list
'Internet Specific 163 23.42 163 23.45 5401.44 30.78 \n'
After I applied strip() and split function, i got this result.
list.strip().split()
['Internet', 'Specific', '163', '23.42', '163', '23.45', '5401.44', '30.78']
But, I want to split the list like this.
['Internet Specific', '163', '23.42', '163', '23.45', '5401.44', '30.78']
I want to split the numeric value but not the alphabet. Is there any way?
Thanks in advance
>Solution :
You can use python re module and re.split() to split string.
Pattern used: \s+(?=\d) [This search for one or more whitespace before digit]
Code:
import re
data = 'Internet Specific 163 23.42 163 23.45 5401.44 30.78'
result = re.split("\s+(?=\d)", data)
print(result)
Output:
['Internet Specific', '163', '23.42', '163', '23.45', '5401.44', '30.78']