How Do I Split A String In Python With Multiple Separators?
Solution 1:
The re.split
is one simple way to do this - in this case, you want to split on the set of separator characters:
>>> import re
>>> thestring = "Breathing 1:-31.145 9:-32.8942 13:-35.8225 2:-35.9872 17:-36.2135 16:-36.6343 12:-36.7487 4:-37.8538 8:-38.6924 7:-39.0389 14:-39.0697 18:-40.0523 3:-40.5393 15:-40.5825 5:-41.6323 11:-45.2976 10:-53.3063 6:-231.617">>> re.split(r"[ :\-]+", thestring)
['Breathing', '1', '31.145', '9', '32.8942', '13', '35.8225', '2', '35.9872', '17', '36.2135', '16', '36.6343', '12', '36.7487', '4', '37.8538', '8', '38.6924', '7', '39.0389', '14', '39.0697', '18', '40.0523', '3', '40.5393', '15', '40.5825', '5', '41.6323', '11', '45.2976', '10', '53.3063', '6', '231.617']
[]
defines a character set, containing a space, :
, and -
(which needs escaped, as it's used for ranges like [a-z]
) - the +
after the character set means one-or-more
To split explicitly on either a space, or :-
, you can use the |
or regex thingy:
>>> re.split(":-| ", thestring)
['Breathing', '1', '31.145', ...]
As I mentioned in the comment on the question, I would have thought the separator would just be :
and the -
indicates a negative number..
Solution 2:
UPDATE: I didn't realize that Breathing
was part of your data. In this case you'll get all strings.
Assuming:
b = 'Breathing 1:-31.145 9:-32.8942 13:-35.8225 2:-35.9872'
then this simple construct:
b.replace(':-',' ').split()
will give:
['Breathing', '1', '31.145', '9', '32.8942', '13', '35.8225', '2', '35.9872']
Explanation: it replaces any :-
with a space (' '
). It then splits the string wherever there is a space to get a list of strings.
To get float
values for the numbers:
['Breathing'] + [float(i) for i in b.replace(':-',' ').split()[1:]]
results in:
['Breathing', 1.0, 31.145, 9.0, 32.8942, 13.0, 35.8225, 2.0, 35.9872]
Explanation: Similar as above, except float()
is used on all of the numeric strings to convert them to floats and the 'Breathing' string is put at the start of the list.
Solution 3:
You can use str.split([sep[, maxsplit]])
Return a list of the words in the string, using sep as the delimiter string. If maxsplit is given, at most maxsplit splits are done (thus, the list will have at most maxsplit+1 elements). If maxsplit is not specified or -1, then there is no limit on the number of splits (all possible splits are made).
Applied
>>' 1 2 3 '.split()
['1', '2', '3']
in "tandem" with str.replace(old, new[, count])
Return a copy of the string with all occurrences of substring old replaced by new. If the optional argument count is given, only the first count occurrences are replaced.
Applied
>>>a = 'h!e!l!l!o! w!o!r!l!d!'>>>a.replace('!','')
'hello world'
Applied to your scenario:
>>'Breathing 1:-31.145 9:-32.8942 13:-35.8225 2:-35.9872 17:-36.2135 16:-36.6343
12:-36.7487 4:-37.8538 8:-38.6924 7:-39.0389 14:-39.0697 18:-40.0523
3:-40.5393 15:-40.5825 5:-41.6323 11:-45.2976 10:-53.3063
6:-231.617'.replace(':-',' ').split(' ')
['Breathing', '1', '31.145', '9', '32.8942', '13', '35.8225', '2',
'35.9872', '17', '36.2135', '16', '36.6343', '12', '36.7487', '4', '37.8538',
'8', '38.6924', '7', '39.0389', '14', '39.0697', '18', '40.0523', '3',
'40.5393', '15', '40.5825', '5', '41.6323', '11', '45.2976',
'10', '53.3063', '6', '231.617']
All the definition are taken from manual
Solution 4:
import re
array=re.split(r'\s+|:-',mystring)
In the regex, \s+
matches whitespace whereas :-
matches that literal sequence in the string. the pipe (|
) is re's way of saying match if either of these conditions matches.
Of course, you could change "\s+"
to "\s"
or even " "
if you wanted to be sure to split on a single space as requested in your question.
Solution 5:
This isn't quite what you asked, but it might be what you need anyway ;-)
lines = ['Breathing 1:-31.145 9:-32.8942 13:-35.8225 2:-35.9872 17:-36.2135 16:-36.6343 12:-36.7487 4:-37.8538 8:-38.6924 7:-39.0389 14:-39.0697 18:-40.0523 3:-40.5393 15:-40.5825 5:-41.6323 11:-45.2976 10:-53.3063 6:-231.617']
data = {}
for line in lines:
line = line.split() # split on spacesvalues = (s.split(':-') for s in line[1:])
data[line[0]] = {int(t):float(val) for t,val in values}
results in
data= {
'Breathing': {
1:31.145,
2:35.9872,
3:40.5393,
4:37.8538,
5:41.6323,
6:231.617,
7:39.0389,
8:38.6924,
9:32.8942,
10:53.3063,
11:45.2976,
12:36.7487,
13:35.8225,
14:39.0697,
15:40.5825,
16:36.6343,
17:36.2135,
18:40.0523
}
}
You can then access it as
data['Breathing'][2] # -> 35.9872
Post a Comment for "How Do I Split A String In Python With Multiple Separators?"