Situation: I have a weather GUI made in tkinter. It gets data from an api and and displays it on a tkinter label. One of the functions 'format_alerts' parses json data from the api. Because the way the data is formatted I'm having trouble parsing it for what I need.
Problem: I came up with a really weird way of parsing the data. The json uses '...' and 'astrix' to separate values in a string (inside a dictionary). I use .replace('\n', ' ') to get rid of newlines. I use .replace('astrix', '@') and .replace('...', '@' to find the split points. Then use .split('@') then reference the list index number. However sometimes the json uses '...' randomly so I end up messing up the indexing. I know regex is a better way to do this but for the life of me I can't get a three part regex search to work.
My present code looks like:
def format_alerts(weather_json):
alert_report = ""
alerts = weather_json['alerts']
try:
# for loop is because sometime there are several different alerts at the list level.
for item in alerts:
event = item['event']
details = item['description']
parsed = details.replace('\n', ' ').replace('*', '@').replace('...', '@').split('@')
# textwrap is used to make sure it fits in my tkinter label
what = textwrap.fill(parsed[4], 51)
where = textwrap.fill(parsed[6], 51)
when = textwrap.fill(parsed[8], 51)
# plugs the textwrapped pieces into a single string.
single_alert = '''{}: {}\nWhere: {}\nWhen: {}\n'''.format(event, what, where, when)
alert_report += single_alert
except:
alert_report = "Alerts Error"
print('ERROR: (format_alerts) retrieving, formatting >>> alert_report ')
return alert_report
weather_json looks like:
{'alerts: [{
'event': 'Small Craft Advisory',
'description':
'...SMALL CRAFT ADVISORY NOW IN EFFECT UNTIL 3 AM PST SATURDAY...\n* WHAT...Rough bar conditions
expected.\n- GENERAL SEAS...Seas 18 to 20 ft today then easing to 14 ft\nlate tonight and
Sat.\n- FIRST EBB...Around 415 AM Fri. Seas near 20 feet with\nbreakers.\n- SECOND EBB...Strong
ebb around 415 PM. Seas near 20 ft\nwith breakers.\n* WHERE...In the Main Channel of the
Columbia River Bar.\n* WHEN...Until 3 AM PST Saturday.\n* IMPACTS...Conditions will be hazardous
to small craft\nespecially when navigating in or near harbor entrances.'
# I addedd newlines so it wasn't massively long. raw data only has newlines denoted by \n
},]}
I want the returned 'alert_report' string to look like this:
'''Small Craft Advisory: Rough bar conditions expected. GENERAL SEAS Seas 18 to 20 ft today
then easing to 14 ft late tonight and Sat. FIRST EBB Around 415 AM Fri. Seas near 20 feet
with SECOND EBB Strong ebb around 415 PM. Seas near 20 ft breakers.
Where: In the Main Channel of the Columbia River Bar
When: Until 3 AM PST Saturday.
Note: My present code worked on 30 some alerts this was the first one that broke my code. I can live without the "GENERAL SEAS Seas 18 to 20 ft ..." in the first line. But I don't want to cut it off at '.' because some alerts are several sentences. I learned regex but I'm not very good with it.