Issue
I have a list like this:
ques = ['Normal adult dogs have how many teeth?', 'What is the most common training command taught to dogs?', 'What is this?;{;i1.png;};', 'Which part of cats is as unique as human fingerprints?', 'What is a group of cats called??', 'What is this?;{;i2.png;};']
As we can see that at ques[2] and ques[5] there is a text at end that follows a specific pattern ;{;*;};
That is the name of a img file stored in the directory. I want to extract those file name i.e. a list that contains :
Img_name = ['i1.png','i2.png']
Also after doing this i want to update ques and remove the pattern and img filename from it.
Solution
use regular expression;
import re
image_names = []
pattern = re.compile(r';{;([\w.]+);};')
for idx, item in enumerate(ques):
result = re.search(pattern, item)
if result:
image_names.append(result.group(1))
ques[idx] = re.sub(pattern, '', item)
print(ques)
print(image_names)
Answered By - 정도유
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.