Extracting Values From String

Question:

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"

Asked By: toolost

||

Answers:

the easiest way:

with ZipFile(zipfile, 'r') as f:
   filelist = list(filter(lambda x: x.endswith(".pdf"),f.namelist()))
   print(filelist)
Answered By: Deni
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.