0

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"

3 Answers3

0

If you are confident that the path will always end with the image filename, and won't have a query string after it (e.g., nt_cloudy.gif?foo=bar&x=y&...) you can just use the filesystem path functions from Python's os.path standard module.

https://docs.python.org/3/library/os.path.html

#!/usr/bin/env python
import os

URL = 'http://icons.wxug.com/i/c/k/nt_cloudy.gif'
FILENAME = os.path.basename(URL)

If you are trying to decode a URL that might include a query string, you may prefer to use the urllib.parse module.

https://docs.python.org/3/library/urllib.parse.html#module-urllib.parse

I won't go into detail about why your regular expression isn't working the way you expect, because honestly, hand-crafting regular expressions is overkill for this use-case.

direvus
  • 362
  • 2
  • 6
0

You could use below regular expresion:

let regex = /(\w+)\.gif/g.exec("http://icons.wxug.com/i/c/k/nt_cloudy.gif")
if(regex != null && regex.length == 2)
    console.log(regex[1]);

Find the reference here.

Cristian Colorado
  • 2,022
  • 3
  • 19
  • 24
0

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"
Kondasamy Jayaraman
  • 1,802
  • 1
  • 20
  • 25