2

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

4
  • rsplit, as the r in the name implies, begins splitting from the right. You are splitting the list by the rightmost _. Try split Commented Apr 21, 2020 at 13:33
  • You are only splitting starting from the right (that's what rsplit does), and setting limit o 1. You've probably copied this code and are unsure of what it does. Check out the string docs, it is quite clear that you need split here Commented Apr 21, 2020 at 13:33
  • What is the problem with something simple like this: cms_name = '187045_20191025_Release_UAT_1.zip'.split('_'), and then print(cms_name[0], cms_name[1]) ? Commented Apr 21, 2020 at 13:34
  • '187045_20191025_Release_UAT_1.zip'.split('.')[0].split('_') Commented Apr 21, 2020 at 13:37

8 Answers 8

2

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']
Sign up to request clarification or add additional context in comments.

Comments

1

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']

Comments

1
text='187045_20191025_Release_UAT_1.zip'
text=text.replace('.zip','')
arr=text.split('_')
print([arr[0],arr[-1]])

Output:

['187045', '1']

Comments

1

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 ;)

Comments

1

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

Comments

1

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']

Comments

1

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']

Comments

0

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']

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.