Having this line:
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
I want to store in an array everything except separators(' ',':-')
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.
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..
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With