Trying to scrape weather condition (index 9 in list v) and save the variable for later. Having difficulty writing the proper regex to store condition that is either 1 or 2 words.
Tested my regex code on regexr.com and it looks fine but doesn't work when run in IDLE.
v = ['\n\n7:53 AM\n\n\n\n\n',
'\n\n\n\n\n\n48 \nF\n \n\n\n\n\n\n\n',
'\n\n\n\n\n\n45 \nF\n \n\n\n\n\n\n\n',
'\n\n\n\n\n\n89 \n%\n \n\n\n\n\n\n\n',
'\n\nSE\n\n\n\n\n',
'\n\n\n\n\n\n5 \nmph\n \n\n\n\n\n\n\n',
'\n\n\n\n\n\n0 \nmph\n \n\n\n\n\n\n\n',
'\n\n\n\n\n\n30.11 \nin\n \n\n\n\n\n\n\n',
'\n\n\n\n\n\n0.0 \nin\n \n\n\n\n\n\n\n',
'\n\nMostly Cloudy\n\n\n\n\n']
for condition in str(v[9]):
condition_search = re.findall('[A-Z]\w+', condition)
if len(condition_search) > 1:
condition = ' '
condition = condition.join(condition_search)
else:
condition = str(condition_search)
print(condition)
actual results:
'[]'
desired results
'Mostly Cloudy'

if re.search(r'^[A-Z]\w*(?:\s+[A-Z]\w*)?$', v[9].strip()): condition = v[9].strip()join()with a list that has only 1 element, so you don't need the test.