0

This is a string: \texample\tDart_181120172410.jpg\tImgCaption\t

Is there anyway to get the Dart_181120172410.jpg and could say to get this substring if only it contains .jpg at the end. The actual string is even longer

1
  • it it tab-delimited (\t), Split by tabs, check each for ending with .jpg, done - see answer Commented Nov 22, 2017 at 6:07

4 Answers 4

2

You can use split()

>>> image_name = s.split('\t')[2]
>>> if '.jpg' in image_name:
        print(image_name)
Dart_181120172410.jpg
Sign up to request clarification or add additional context in comments.

Comments

1

Like this:

s = "a.jpg\tnot.llm\tp.jpg\tc.jpg\te.gif\tnix.txt"

all_jpegs = [x for x in s.split('\t') if '.jpg' in x]

print(all_jpegs)

Output: ['a.jpg', 'p.jpg', 'c.jpg']

guillaume-dedrie made a good point in the comment - this will lead to false positives for s=some.file\tthisisno.jgp.gif\tsomemore.files. Changing it to

s = "a.jpg\tnot.llm\tp.jpg\tc.jpg\te.gif\tnix.txt\tnot.jpg.gif\tthis.JPG"
better_jpegs = [x for x in s.split('\t') if x.lower().endswith('.jpg')]

print(better_jpegs)

would eleminate that and also handle '.JpG' or '.JPG'

1 Comment

Thank you I was looking for this.
0

You can use regex in python for this.

Comments

0

You can try this simple regex

\w+.(?:jpg)

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.