1

I have some folders like-so:

../dog_breeds/images/Images/n02085620-Chihuahua
../dog_breeds/images/Images/n02085782-Japanese_spaniel
../dog_breeds/images/Images/n02086910-papillon
../dog_breeds/images/Images/n02088466-bloodhound
....
....

I want to extract only these info's(Chihuahua, Japanese_spaniel, papillon, bloodhound) from the paths of the files using Python. Can anybody help me ?

2 Answers 2

1

You can use str.split here

Ex:

s = """../dog_breeds/images/Images/n02085620-Chihuahua
../dog_breeds/images/Images/n02085782-Japanese_spaniel
../dog_breeds/images/Images/n02086910-papillon
../dog_breeds/images/Images/n02088466-bloodhound
"""

for p in s.splitlines():
    print(p.split("-")[-1])

If you need regex.

import re

for p in s.splitlines():
    print(re.search(r"\-(\w+)$", p).group(1))

Output:

Chihuahua
Japanese_spaniel
papillon
bloodhound
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks I was actually looking for a regex solution.
1

There are two common factors.

  • Image folder paths are same

    • For instance: all image starts with ../dog_breeds/images/Images/
    • replace('../dog_breeds/images/Images/', '') for removing the paths
  • All images start with 10-character

    • For instance: n02085620-, n02085782-
    • replace('../dog_breeds/images/Images/', '')[10:] for removing the characters.

If we combine the two factors:

res = ['../dog_breeds/images/Images/n02085620-Chihuahua',
       '../dog_breeds/images/Images/n02085782-Japanese_spaniel',
       '../dog_breeds/images/Images/n02086910-papillon',
       '../dog_breeds/images/Images/n02088466-bloodhound']

part1 = [r.replace('../dog_breeds/images/Images/', '')[10:] for r in res]
print(part1)

Output is:

['Chihuahua', 'Japanese_spaniel', 'papillon', 'bloodhound']

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.