0

In the following string how to get the id after the directory media and after getting the id ignore the rest of the string to read only the id numbers

id_arr= ["/opt/media/12/htmls","/opt/media/24/htmls","/opt/media/26/htmls","/opt/media/56/htmls"]

The output should be 12 24 26 56

5 Answers 5

3

If the strings always look the way you said, try

ids = [int(s.split("/")[3]) for s in id_arr]
Sign up to request clarification or add additional context in comments.

2 Comments

Maybe use os.path.sep instead of a plain /, just in case?
Not sure if these are paths or parts of URLs.
3
>>> import re
>>> myre = re.compile("^.*/media/(\d+)")
>>> for item in id_arr:
...     print (myre.search(item).group(1))
...
12
24
26
56

1 Comment

Why the downvote? Yeah, regexes are bit overkill, but they satisfy the requirement that we're extracting the numerical ID after the media directory. No other solution considers the possibility that it might not be the third field (not even Puller's, since it assumes the prefix is /opt/media).
0

[x.split('/')[3] for x in id_arr]

Comments

0

The correct way probably involves some clever use of the os.path module, but for the input given, just use a regex for media\/([0-9]+) and extract the first group.

Comments

0
parts = "/opt/media/12/htmls","/opt/media/24/htmls","/opt/media/26/htmls","/opt/media/56/htmls"
for str in parts:
    print str.split("/")[3]

EDIT: unuseful rpartition() removed

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.