How to extract specific path from string using Python? [duplicate] - python

This question already has answers here:
s3 urls - get bucket name and path
(11 answers)
Closed 2 years ago.
i have string name Path:
Path = 's3://ihsm-dl-automotive-dev/auto_target_marketing_data/Testing-sandbox/PP_PROCESS_TESTING_DEEPAK/Buick_Encore_studyfile'
i want to create two string and store in two different variables as given below;
bucket = 'ihsm-dl-automotive-dev'
path_new = 'auto_target_marketing_data/Testing-sandbox/PP_PROCESS_TESTING_DEEPAK/Buick_Encore_studyfile'
i have tried few option like split by delimiter like Path.split('/') but it gives me in list form.
can anyone help me with this?

If you already have a constant bucket name, why not split with that name and use that to split and then slice the string with :
Path.split(bucket)[1][1:]
OR
If you dont have a constant bucket string, use:
a=Path.split("/")
bucket,path=a[2],"/".join(a[3:])
This will give you a list, but you can join them using join

Related

Make a list with multiple possible strings from file names with regex [duplicate]

This question already has answers here:
Regex match one of two words
(2 answers)
Closed 3 years ago.
I want to make a list of several PNG in a folder based on multiple references. So in the list I want the PNG that have the string "7029113" OR "7031503" in their name. This is what I got so far, I only need to know how to do OR with regex, and probably my wildcards are wrong too I'm not sure.
render_path = "C:/BatchRender/Renaming"
os.chdir(render_path)
list_files = glob.glob("*.png")
r = re.compile(".*7029113.*" OR ".*7031503.*")
list_40 = list(filter(r.match, list_files))
This is one way of doing it.
r = re.compile('.*(7029113|7031503).*')

How to extract the filename from a string using regular expression [duplicate]

This question already has answers here:
Extract file name from path, no matter what the os/path format
(22 answers)
Extract file name with a regular expression
(3 answers)
Get Filename Without Extension in Python
(6 answers)
Extracting extension from filename in Python
(33 answers)
Regex: Get Filename Without Extension in One Shot?
(10 answers)
Closed 3 years ago.
I am new in the regular expression and trying to extract the file name from a string which is basically a file path.
string = "input_new/survey/argentina-attributes.csv"
string_required = argentina-attributes
i know i can do this by below code.
string.split('/')[2].split('.')[0]
but I am looking to do this by using regular expression so if in future the formate of the path changes(input_new/survey/path/path/argentina-attributes.csv) should not affect the output.
i know kind of similar question asked before but I am looking for a pattern which will work for my use case.
Try this,
>>> import re
>>> string = "input_new/survey/argentina-attributes.csv"
Output:
>>> re.findall(r'[^\/]+(?=\.)',string) # or re.findall(r'([^\/]+)\.',string)
['argentina-attributes']
Referred from here
Try this:
string = "input_new/survey/argentina-attributes.csv"
new_string = string.split('/')[-1].split('.')[0]
print(new_string)

Filter list and print whole name containing string python [duplicate]

This question already has answers here:
Filtering a list of strings based on contents
(5 answers)
Closed 3 years ago.
I am trying to filter a list in python.
What i need is a way to limit what is beeing printed out from the list
Example:
I want to find all subjects containing "ECON"
from this list:
List = ["INFO100","INFO104","INFO110","INFO150","INFO125","ECON100", "ECON102"]
And i want to be able to print out the full name of the objects containing "ECON" (that means i want it to return "ECON100", "ECON102")
is there an easy way to do this?
for sub_string in List:
if "ECON" in sub_string:
print(sub_string)

How can I split string to several folders? [duplicate]

This question already has answers here:
How to split a dos path into its components in Python
(23 answers)
Closed 4 years ago.
I have a case like this:
Path = 'C:\Intel\ExtremeGraphics\CUI\Resource'
I want to split this string to list with the string folders names:
['C:', 'Intel', 'ExtremeGraphics', 'CUI, 'Resource']
How can I do that in the shortest way?
folders = Path.split('\')
It will return you the required list of the folders

Opening files with the name of a variable [duplicate]

This question already has answers here:
String formatting: % vs. .format vs. f-string literal
(16 answers)
Closed 4 years ago.
I am using a Markov chain. When the chain arrives at a particular state, two files (a .png and an .mp3) need to open.
s is the current state of the chain, an integer from 1-59.
I can't seem to find how to open the file with the same number as 's'.
I'm sure it has something to do with %str formatting, but I can't seem to implement it.
img = Image.open('/.../.../s.png')
img.show()
You should use the following line in your code:
img = Image.open('/.../.../{0}.png'.format(s))
You can format a string using a variable like this
>>> s = 10
>>> '/path/to/file/{}.png'.format(s)
'/path/to/file/10.png'

Categories