Search code examples
stringsplitweatherstrsplit

I need to pull a specific string from a URL path


I am pulling the following URL from a JSON on the internet.

Example of the string I am working with: http://icons.wxug.com/i/c/k/nt_cloudy.gif

I need to get just the "nt_cloudy" from the above in order to write the img (already stored) to an epaper display for a weather app. I have tried re.split() but only ever get the full string back, no matter what I split on.

Everything else works, if I manually enter the filename, I can display the image, however the weather conditions change, so I need to pull the name from the JSON. Again, it is only locating the specific string within the full string I am stuck on.

imgurl = weatherinfo['current_observation']['icon_url'] # http://icons.wxug.com/i/c/k/nt_cloudy.gif
img_condition = re.split('\/ |\// |.', imgurl)
image_1 = "/home/pi/epaper/python2/icons/" + img_condition + ".bmp"

Solution

  • Please check this,

    import re
    
    imgurl = weatherinfo['current_observation']['icon_url'] # http://icons.wxug.com/i/c/k/nt_cloudy.gif
    img_condition = re.split('\/', imgurl)[-1]
    image_1 = "/home/pi/epaper/python2/icons/" + img_condition[:-4] + ".bmp"