Reputation: 3299
Sorry for the lack of better title.
Hopefully the explanation below help. Given the following string:
f=' Sleep stage W\\x14\\x00+26070\\x1590\\x14 Sleep stage W\\x14\\x00+26070\\x1590\\x14 Movement time\\x14\\x00+28110\\x15120\\x14 Sleep stage 3\\x14\\x00+28230\\x1530\\x14'
and are to be split as below
' W\\x14\\x00+26070\\x1590\\x14 '
' W\\x14\\x00+26070\\x1590\\x14 '
' \\x14\\x00+28110\\x15120\\x14 '
' 3\\x14\\x00+28230\\x1530\\x14'
To realise this, the following code was drafted
f=' Sleep stage W\\x14\\x00+26070\\x1590\\x14 Sleep stage W\\x14\\x00+26070\\x1590\\x14 Movement time\\x14\\x00+28110\\x15120\\x14 Sleep stage 3\\x14\\x00+28230\\x1530\\x14'
raw_hypno = [x for x in f.split('Sleep stage')][1:]
which produce the following Output
' W\\x14\\x00+26070\\x1590\\x14 '
' W\\x14\\x00+26070\\x1590\\x14 Movement time\\x14\\x00+28110\\x15120\\x14 '
' 3\\x14\\x00+28230\\x1530\\x14'
As can be seen, the detail Movement time
was not properly split.
May I know whether it is possible to assign two split condition
using the split
approach?
Upvotes: 0
Views: 60
Reputation: 27567
You can first replace all the 'Movement time'
into 'Sleep stage'
,
then simply split by 'Sleep stage'
:
f=' Sleep stage W\\x14\\x00+26070\\x1590\\x14 Sleep stage W\\x14\\x00+26070\\x1590\\x14 Movement time\\x14\\x00+28110\\x15120\\x14 Sleep stage 3\\x14\\x00+28230\\x1530\\x14'.replace('Movement time','Sleep stage').split('Sleep stage')
Upvotes: 1
Reputation: 82765
Using Regex --> re.split
.
Ex:
f=' Sleep stage W\\x14\\x00+26070\\x1590\\x14 Sleep stage W\\x14\\x00+26070\\x1590\\x14 Movement time\\x14\\x00+28110\\x15120\\x14 Sleep stage 3\\x14\\x00+28230\\x1530\\x14'
print(re.split(r"Sleep stage|Movement time", f))
Output:
[' ', ' W\\x14\\x00+26070\\x1590\\x14 ', ' W\\x14\\x00+26070\\x1590\\x14 ', '\\x14\\x00+28110\\x15120\\x14 ', ' 3\\x14\\x00+28230\\x1530\\x14']
Upvotes: 2