Home > OS >  Filter specific word in string using python
Filter specific word in string using python

Time:11-15

I got some data like this

https://www.travel.taipei/d_upload_ttn/sceneadmin/pic/11000358.jpghttps://www.travel.taipei/d_upload_ttn/sceneadmin/image/a0/b0/c0/d756/e285/f317/2ece2309-3d1c-49da-8d3a-32e0227e7732.jpghttps://www.travel.taipei/d_upload_ttn/sceneadmin/image/a0/b0/c1/d379/e118/f25/554586cb-cf2d-40ef-9b6a-55fcf8d9e598.jpghttps://www.travel.taipei/d_upload_ttn/sceneadmin/image/a0/b0/c1/d856/e130/f366/21ed2d17-7610-4ad2-b517-5b1b0007612a.jpghttps://www.travel.taipei/d_upload_ttn/sceneadmin/pic/11000360.jpghttps://www.travel.taipei/d_upload_ttn/sceneadmin/image/a0/b0/c1/d356/e17/f185/b1a2de52-4110-4355-a9fb-bf1d0eb627c9.jpghttps://www.travel.taipei/d_upload_ttn/sceneadmin/image/a0/b0/c0/d593/e103/f285/1633c311-e148-4d03-bb43-292d816951d2.jpghttps://www.travel.taipei/d_upload_ttn/sceneadmin/pic/11000359.jpghttps://www.travel.taipei/streams/scenery_file_audio/c03.mp3

The thing I want to do is to put URL that contains "jpg" or "png" into a list by using Python.
like["https.....jpg", "https......jpg", "https........png"]
But I have no ideas. Any suggestions?

CodePudding user response:

Try:

s = """https://www.travel.taipei/d_upload_ttn/sceneadmin/pic/11000358.jpghttps://www.travel.taipei/d_upload_ttn/sceneadmin/image/a0/b0/c0/d756/e285/f317/2ece2309-3d1c-49da-8d3a-32e0227e7732.jpghttps://www.travel.taipei/d_upload_ttn/sceneadmin/image/a0/b0/c1/d379/e118/f25/554586cb-cf2d-40ef-9b6a-55fcf8d9e598.jpghttps://www.travel.taipei/d_upload_ttn/sceneadmin/image/a0/b0/c1/d856/e130/f366/21ed2d17-7610-4ad2-b517-5b1b0007612a.jpghttps://www.travel.taipei/d_upload_ttn/sceneadmin/pic/11000360.jpghttps://www.travel.taipei/d_upload_ttn/sceneadmin/image/a0/b0/c1/d356/e17/f185/b1a2de52-4110-4355-a9fb-bf1d0eb627c9.jpghttps://www.travel.taipei/d_upload_ttn/sceneadmin/image/a0/b0/c0/d593/e103/f285/1633c311-e148-4d03-bb43-292d816951d2.jpghttps://www.travel.taipei/d_upload_ttn/sceneadmin/pic/11000359.jpghttps://www.travel.taipei/streams/scenery_file_audio/c03.mp3"""

for url in s.split("http"):
    if url.endswith(("jpg", "png")):
        print("http"   url)

Prints:

https://www.travel.taipei/d_upload_ttn/sceneadmin/pic/11000358.jpg
https://www.travel.taipei/d_upload_ttn/sceneadmin/image/a0/b0/c0/d756/e285/f317/2ece2309-3d1c-49da-8d3a-32e0227e7732.jpg
https://www.travel.taipei/d_upload_ttn/sceneadmin/image/a0/b0/c1/d379/e118/f25/554586cb-cf2d-40ef-9b6a-55fcf8d9e598.jpg
https://www.travel.taipei/d_upload_ttn/sceneadmin/image/a0/b0/c1/d856/e130/f366/21ed2d17-7610-4ad2-b517-5b1b0007612a.jpg
https://www.travel.taipei/d_upload_ttn/sceneadmin/pic/11000360.jpg
https://www.travel.taipei/d_upload_ttn/sceneadmin/image/a0/b0/c1/d356/e17/f185/b1a2de52-4110-4355-a9fb-bf1d0eb627c9.jpg
https://www.travel.taipei/d_upload_ttn/sceneadmin/image/a0/b0/c0/d593/e103/f285/1633c311-e148-4d03-bb43-292d816951d2.jpg
https://www.travel.taipei/d_upload_ttn/sceneadmin/pic/11000359.jpg

CodePudding user response:

replace and split

strs ="https://www.travel.taipei/d_upload_ttn/sceneadmin/pic/11000358.jpghttps://www.travel.taipei/d_upload_ttn/sceneadmin/image/a0/b0/c0/d756/e285/f317/2ece2309-3d1c-49da-8d3a-32e0227e7732.jpghttps://www.travel.taipei/d_upload_ttn/sceneadmin/image/a0/b0/c1/d379/e118/f25/554586cb-cf2d-40ef-9b6a-55fcf8d9e598.jpghttps://www.travel.taipei/d_upload_ttn/sceneadmin/image/a0/b0/c1/d856/e130/f366/21ed2d17-7610-4ad2-b517-5b1b0007612a.jpghttps://www.travel.taipei/d_upload_ttn/sceneadmin/pic/11000360.jpghttps://www.travel.taipei/d_upload_ttn/sceneadmin/image/a0/b0/c1/d356/e17/f185/b1a2de52-4110-4355-a9fb-bf1d0eb627c9.jpghttps://www.travel.taipei/d_upload_ttn/sceneadmin/image/a0/b0/c0/d593/e103/f285/1633c311-e148-4d03-bb43-292d816951d2.jpghttps://www.travel.taipei/d_upload_ttn/sceneadmin/pic/11000359.jpghttps://www.travel.taipei/streams/scenery_file_audio/c03.mp3"
strs =strs.replace("jpg", 'jpg ')
strs =strs.replace("png", 'png ')
print(strs.split())

output #

['https://www.travel.taipei/d_upload_ttn/sceneadmin/pic/11000358.jpg', 'https://www.travel.taipei/d_upload_ttn/sceneadmin/image/a0/b0/c0/d756/e285/f317/2ece2309-3d1c-49da-8d3a-32e0227e7732.jpg', 'https://www.travel.taipei/d_upload_ttn/sceneadmin/image/a0/b0/c1/d379/e118/f25/554586cb-cf2d-40ef-9b6a-55fcf8d9e598.jpg', 'https://www.travel.taipei/d_upload_ttn/sceneadmin/image/a0/b0/c1/d856/e130/f366/21ed2d17-7610-4ad2-b517-5b1b0007612a.jpg', 'https://www.travel.taipei/d_upload_ttn/sceneadmin/pic/11000360.jpg', 'https://www.travel.taipei/d_upload_ttn/sceneadmin/image/a0/b0/c1/d356/e17/f185/b1a2de52-4110-4355-a9fb-bf1d0eb627c9.jpg', 'https://www.travel.taipei/d_upload_ttn/sceneadmin/image/a0/b0/c0/d593/e103/f285/1633c311-e148-4d03-bb43-292d816951d2.jpg', 'https://www.travel.taipei/d_upload_ttn/sceneadmin/pic/11000359.jpg', 'https://www.travel.taipei/streams/scenery_file_audio/c03.mp3']
  • Related