2

I have strings like :

's3://bukcet_name/tables/name=moonlight/land/timestamp=2020-06-25 01:00:23.180745/year=2019/month=5'

And from it I would like to obtain a tuple contain the year value and the month value as first and second element of my tuple.

('2019', '5')

For now I did this :

([elem.split('=')[-1:][0] for elem in part[0].split('/')[-2:]][0], [elem.split('=')[-1:][0] for elem in part[0].split('/')[-2:]][1])

It isn't very elegant, how could I do better ?

1
  • consider using the re package for python with all its functionality. Commented Jun 25, 2020 at 9:55

2 Answers 2

3

Use, re.findall along with the given regex pattern:

import re
matches = re.findall(r'(?i)/year=(\d+)/month=(\d+)', string)

Result:

# print(matches)
[('2019', '5')]

Test the regex pattern here.

Sign up to request clarification or add additional context in comments.

Comments

-1

Perhaps regular expressions could do it. I would use regular expressions to capture the strings 'year=2019' and 'month=5' then return the item at index [-1] by splitting these two with the character '='. Hold on, let me open up my Sublime and try to write actual code which suits your specific case.

import re  
search_string = 's3://bukcet_name/tables/name=moonlight/land/timestamp=2020-06-25 01:00:23.180745/year=2019/month=5'  
string1 = re.findall(r'year=\d+', search_string)  
string2 = re.findall(r'month=\d+', search_string)  
result = (string1[0].split('=')[-1], string2[0].split('=')[-1]) print(result) 

1 Comment

import re search_string = 's3://bukcet_name/tables/name=moonlight/land/timestamp=2020-06-25 01:00:23.180745/year=2019/month=5' string1 = re.findall(r'year=\d+', search_string) string2 = re.findall(r'month=\d+', search_string) result = (string1[0].split('=')[-1], string2[0].split('=')[-1]) print(result) Hope it helps.

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.