I have a string below which altogether joined by underscores _
that i want to split in such a way to get my desired output.
Below is list string:
>>> a
'cDOT_stv3027_esx_vdi01_07-24-2021_02.00.00.0443'
>>> type(a)
<type 'str'>
Simple rsplit()
operation by 2
which turns it into 3 different list values as shown below, like from the end its time
, date
and then one combine strings ie 'cDOT_stv3027_esx_vdi01'
which i want to split into two parts like 'cDOT'
& 'stv3027_esx_vdi01'
.
>>> a.rsplit("_",2)
['cDOT_stv3027_esx_vdi01', '07-24-2021', '02.00.00.0443']
I am trying below on the first index but then i'll not retain rest of values.
>>> a.rsplit("_",2)[0].split("_",1)
['cDOT', 'stv3027_esx_vdi01']
My desired output should be like below:
['cDOT', 'stv3027_esx_vdi01', '07-24-2021', '02.00.00.0443']
You can use
a = 'cDOT_stv3027_esx_vdi01_07-24-2021_02.00.00.0443'
prefix, *mid, date, time = a.split('_')
print(prefix, '_'.join(mid), date, time)
See the online Python demo.
In this case, you can have as many underscore separated parts between the prefix and date as there are.
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments