Untitled

mail@pastecode.io avatar
unknown
plain_text
13 days ago
1.5 kB
2
Indexable
Never
#!/usr/bin/env python3
import sys
from collections import defaultdict

def main():
    station_data = defaultdict(lambda: {'count': 0, 'temp_sum': 0.0, 'hum_sum': 0.0, 'press_sum': 0.0})

    for line in sys.stdin:
        line = line.strip()
        if not line:
            continue

        # Split the line into fields
        fields = line.split('\t')
        if len(fields) != 5:
            continue

        station_id, date_time, temperature, humidity, pressure = fields

        try:
            temperature = float(temperature)
            humidity = float(humidity)
            if pressure:
                pressure = float(pressure)
            else:
                pressure = None
        except ValueError:
            continue  # Skip lines with conversion errors

        # Update station data
        station_data[station_id]['count'] += 1
        station_data[station_id]['temp_sum'] += temperature
        station_data[station_id]['hum_sum'] += humidity
        if pressure is not None:
            station_data[station_id]['press_sum'] += pressure

    # Output the results
    for station_id, stats in station_data.items():
        count = stats['count']
        avg_temp = stats['temp_sum'] / count if count > 0 else None
        avg_hum = stats['hum_sum'] / count if count > 0 else None
        avg_press = stats['press_sum'] / count if count > 0 else None
        
        print(f"{station_id}\t{avg_temp:.2f}\t{avg_hum:.2f}\t{avg_press:.2f}")

if __name__ == "__main__":
    main()
Leave a Comment