0

I am getting a named list as an output from a Python program.

[{'path': u'/home/mycomp/Documents/folder1/906350_16379886.JPG', 'score': 7.937254, 'dist': 0.0, 'id': u'AWW3yZOubIicFyUL0_lv', 'metadata': None}, {'path': u'/home/mycomp/Documents/folder1/907675_16389607.JPG', 'score': 0.5119519, 'dist': 0.2922064602034243, 'id': u'AWW3bhtybIicFyUL0uVN', 'metadata': None}]

The length of the output in this case is 2 and I do not have control on the length of the output. I want to get only the filename from the path printed. I understand i can use

os.path.basename(path)

to get the filename. However, when I tried getting the path from the list using the following code:

for (i, imagePath) in enumerate(imagePaths):
    a=ses.search_image(imagePath)
    for k,v in enumerate(a):
        print(v)    

imagePath = provides the path to the image

a=ses.search_image(imagePath) = searches for similar image and stores all the matching image parameters for the image in the path to a. That output of a is what I have given above.

for k,v in enumerate(a):
     print(v)

actually prints each list as a separate line like this:

{'path': u'/home/mycomp/Documents/folder1/906350_16379886.JPG', 'score': 7.937254, 'dist': 0.0, 'id': u'AWW3yZOubIicFyUL0_lv', 'metadata': None}
{'path': u'/home/mycompDocuments/folder1/907675_16389607.JPG', 'score': 0.5119519, 'dist': 0.2922064602034243, 'id': u'AWW3bhtybIicFyUL0uVN', 'metadata': None}

The desired output is just this:

906350_16379886.JPG
907675_16389607.JPG
3
  • 1
    Sorry to bother you, but there is no such thing as a "named list" in python. The item in your first example is a list with two elements. Also, you say "prints each list...". Those are not lists, they are dictionaries. Dictionaries and lists are different types of objects in python. Following the naming conventions will surely make solving problems easier in the future. Commented Sep 10, 2018 at 8:04
  • @Chillie Thank you for the clarification...I am on learning path of programming...got lost with the nomenclature. will be careful next time. Commented Sep 10, 2018 at 14:39
  • Good luck! Make sure you read some of the official documentation it has plenty of useful information with examples. Commented Sep 10, 2018 at 15:03

2 Answers 2

2

Use os.basename to extract the filename, and grab the path key and not the entire dictionary. (Also, don't see a need to use enumerate in here)

import os # goes at the top of your script
for v in a:
    print(os.path.basename(v['path']))
Sign up to request clarification or add additional context in comments.

Comments

0

Without importing os:

lst = [{'path': u'/home/mycomp/Documents/folder1/906350_16379886.JPG', 'score': 7.937254, 'dist': 0.0, 'id': u'AWW3yZOubIicFyUL0_lv', 'metadata': None}, {'path': u'/home/mycomp/Documents/folder1/907675_16389607.JPG', 'score': 0.5119519, 'dist': 0.2922064602034243, 'id': u'AWW3bhtybIicFyUL0uVN', 'metadata': None}]

print([x['path'].rsplit('/', 1)[1] for x in lst])
# ['906350_16379886.JPG', '907675_16389607.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.