yujuezhao
yujuezhao

Reputation: 1105

How to choose a certain position to split a string by "_"?

I have a string like this '00004079_20150427_5_169_192_114.npz', and I want to split it into this ['00004079_20150427_5', '169_192_114.npz'].

I tried the Python string split() method:

a = '00004079_20150427_5_169_192_114.nii.npz'
a.split("_", 3)

but it returned this:

['00004079', '20150427', '5', '169_192_114.nii.npz']

How can I split this into 2 parts by the third "_" appearance?

I also tried this:

reg = ".*\_.*\_.\_"
re.split(reg, a)

but it returns:

['', '169_192_114.nii.npz']

Upvotes: 1

Views: 124

Answers (2)

Sunitha
Sunitha

Reputation: 12015

You can split the string based on the delimiter _ upto 3 times and then join back everything except the last value

>>> *start, end = s.split('_', 3)
>>> start = '_'.join(start)
>>> 
>>> start
'00004079_20150427_5'
>>> end
'169_192_114.npz'

For python2, you can follow this instead

>>> lst = s.split('_', 3)
>>> end = lst.pop()
>>> start = '_'.join(lst)
>>> 
>>> start
'00004079_20150427_5'
>>> end
'169_192_114.npz'

Upvotes: 4

RomanPerekhrest
RomanPerekhrest

Reputation: 92854

One of possible approaches (if going with regex):

import re

s = '00004079_20150427_5_169_192_114.nii.npz'
res = re.search(r'^((?:[^_]+_){2}[^_]+)_(.+)', s)
print(res.groups())

The output:

('00004079_20150427_5', '169_192_114.nii.npz')

Upvotes: 1

Related Questions