Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Extracting Values From String

I have a string generated from namelist() of ZipFile and I want to save in a variable only the filename of the pdf file:

with ZipFile(zipfile, 'r') as f:
    filelist = f.namelist()
    print(filelist)

The output is: ['test.pdf', 'image_3.png', 'image-1.jpg', 'text-2.txt']

For example I want to store in the variable pdfname the value "test.pdf"

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

the easiest way:

with ZipFile(zipfile, 'r') as f:
   filelist = list(filter(lambda x: x.endswith(".pdf"),f.namelist()))
   print(filelist)
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading