Reputation: 1195
I have a file named 187045_20191025_Release_UAT_1.zip
. I want to get 187045
and 1
at the end.
I have used this code snippet, but it is not giving me what I want.
cms_name = splitext(split('187045_20191025_Release_UAT_1.zip')[1])[0].rsplit('_', 1)
print(cms_name)
This is the output.
['187045_20191025_Release_UAT', '1']
cms_name
must give me just 187045
. It is obvious that I am making a mistake somewhere.
Please guide me.
Regards
Upvotes: 2
Views: 1379
Reputation: 54
if you assign your string as an variable called a, shown below:
a = "187045_20191025_Release_UAT_1.zip"
you can get what you desire by this code below:
lis = [a.split("_")[0], a.split("_")[-1][:-4]]
which in this actually we have a.split("_") as:
a.split("_") = ['187045', '20191025', 'Release', 'UAT', '1.zip']
Upvotes: 1
Reputation: 1328
You can also use something like this
text='187045_20191025_Release_UAT_1.zip'
arr = text.replace('.zip', '').split('_')
>>> arr[::len(arr)-1]
['187045', '1']
Upvotes: 0
Reputation: 42139
I believe unpacking would give a more elegant solution if you regularize the separators (i.e. "." as "_"):
fileName = '187045_20191025_Release_UAT_1.zip'
reqId,*_,testId,_ = fileName.replace(".","_").split("_")
output:
print([reqId,testId])
['187045', '1']
Upvotes: 1
Reputation: 15588
You can also use list unpacking
g = '187045_20191025_Release_UAT_1.zip'
first, *middle, last = g.replace('.zip','').split('_')
print([first, last])
We replace '.zip' with nothing, split with _
. This returns a list to which you can get first and last items. If you are not going to use the middle
which is a list of all values in the middle, you can just do *_
as a throwaway variable
Upvotes: 1
Reputation: 514
Really simple:
import os
my_file_name = os.path.basename("test_x_y_1.png")
my_file_name = my_file_name.split("_")
my_file_name[len(my_file_name)-1] = my_file_name[len(my_file_name)-1].split(".")
what_you_wanna_get = list((my_file_name[0], my_file_name[len(my_file_name)-1][0]))
print(what_you_wanna_get)
The code is very basic and can be optimized but you got the spirit... Use split to separate your string everytime a "_" or a "." or whatever you want appear ;)
Upvotes: 1
Reputation: 1964
text='187045_20191025_Release_UAT_1.zip'
text=text.replace('.zip','')
arr=text.split('_')
print([arr[0],arr[-1]])
Output:
['187045', '1']
Upvotes: 1
Reputation: 21
You can either use regex or split, here is an example using split:
s='187045_20191025_Release_UAT_1.zip'
cms_name=[s.split('_')[0], s.split('_')[-1].split('.')[0]]
print(cms_name)
This is the result:
['187045', '1']
Upvotes: 2
Reputation: 13413
Basically your filename is joined by _
, so you want to split by that,
also obviously you want the first and last parts,
To get the first part just take the first element of the split,
the last part also has the file extension attached, so you can get rid of that by splitting on the .
and taking the first part of that split.
try this:
parts = '187045_20191025_Release_UAT_1.zip'.split('_')
cms_name = parts[0]
the_one_at_the_end = parts[-1].split('.')[0]
print([cms_name, the_one_at_the_end])
Output:
['187045', '1']
Upvotes: 1