diff --git a/covid.py b/covid.py index ade4946..472ab25 100644 --- a/covid.py +++ b/covid.py @@ -11,6 +11,25 @@ import datetime from html import escape from tabulate import tabulate +def clean_text(text): + """Clean text by removing problematic characters that cause encoding issues.""" + if text is None: + return '' + + # Convert to string if not already + text = str(text) + + # Remove the problematic 'Â' character that appears with degree symbols + text = text.replace('\u00a0', ' ') # Non-breaking space to regular space + text = text.replace('\xc2', '') # Remove standalone  character + text = text.replace('°', '°') # Fix degree symbol encoding issue + text = text.replace('°F', '°F') # Fix degree F encoding issue + text = text.replace('°C', '°C') # Fix degree C encoding issue + text = text.replace('\u00a0', ' ') # Remove non-breaking spaces + text = text.encode('utf-8', errors='ignore').decode('utf-8') # Handle other encoding issues + + return text + allobs = [] states = ['wv', 'oh', 'va', 'ky'] @@ -25,7 +44,7 @@ try: response = requests.get(url) response.raise_for_status() # Check for HTTP errors data = xmltodict.parse(response.content.decode('utf-8')) # Explicitly decode as UTF-8 - + try: daily_reports = data.get('Cocorahs', {}).get('DailyPrecipReports') if daily_reports is None: @@ -55,13 +74,13 @@ try: finalobs = [] for obs in allobs: tempob = [ - obs.get('DateTimeStamp', ''), - obs.get('StationNumber', ''), - obs.get('StationName', ''), - obs.get('TotalPrecipAmt', ''), - obs.get('NewSnowDepth', ''), - obs.get('TotalSnowDepth', ''), - obs.get('Notes', '') + clean_text(obs.get('DateTimeStamp', '')), + clean_text(obs.get('StationNumber', '')), + clean_text(obs.get('StationName', '')), + clean_text(obs.get('TotalPrecipAmt', '')), + clean_text(obs.get('NewSnowDepth', '')), + clean_text(obs.get('TotalSnowDepth', '')), + clean_text(obs.get('Notes', '')) ] finalobs.append(tempob) @@ -78,27 +97,30 @@ try: html_content = """
+| Date/Time of Ob (Z) | -Station Number | -Station Name | -New Precip | -New Snow | -Snow Depth | -Comments | +Date/Time of Ob (Z) | +Station Number | +Station Name | +New Precip | +New Snow | +Snow Depth | +Comments |
|---|