如何将deque转换为JSON?并处理服务器接收的海洋观测数据
Got it, let's walk through how to tackle your marine observation data processing and deque-to-JSON conversion. Here's a practical, Python-based solution tailored to your needs:
1. First: Fix Raw Data Formatting Quirks
Your incoming data has a few inconsistencies that need cleaning before we can turn it into valid, structured JSON:
- Decimal values use commas (e.g.,
12,217instead of12.217) - Date format is
dd/mm/yyyybut your field expectsyyyy-mm-dd - Time values have commas for milliseconds (e.g.,
08:04,3instead of08:04.3)
2. Convert Deque to Structured JSON
Python makes this straightforward since deques are iterable, and we can pair them with your field names to build valid JSON objects. Here's a complete code example:
import json import collections from datetime import datetime def preprocess_marine_data(raw_rows, field_names): processed_entries = [] for row in raw_rows: # Skip rows that don't match field count (handle edge cases) if len(row) != len(field_names): print(f"Skipping invalid row: {row}") continue # Fix date format: dd/mm/yyyy → yyyy-mm-dd raw_date = row[0] try: parsed_date = datetime.strptime(raw_date, "%d/%m/%Y") standardized_date = parsed_date.strftime("%Y-%m-%d") except ValueError: print(f"Invalid date format: {raw_date}") continue # Fix time format: replace comma with decimal point standardized_time = row[1].replace(",", ".") # Convert all numeric fields (index 2 onwards) to floats, fixing commas try: numeric_values = [float(val.replace(",", ".")) for val in row[2:]] except ValueError as e: print(f"Failed to parse numeric values in row {row}: {e}") continue # Combine into a dictionary matching your field names entry = dict(zip(field_names, [standardized_date, standardized_time] + numeric_values)) processed_entries.append(entry) return processed_entries # Example usage with your deque of data fields = ['Date(yyyy - mm - dd)', 'Time', 'Conductivity(mS / cm)', 'Temperature(C)', 'Depth(m)', 'Battery(V)', 'Salinity(PSU)', 'Density (kg m-3)', 'Calc, SV (m/s)'] # Your deque containing raw data rows raw_data_deque = collections.deque([ ['18/01/2017', '08:04,3', '12,217', '8,701', '2,1', '7,6', '10,453', '1008,002', '1455,04'], ['18/01/2017', '08:04,8', '12,256', '8,695', '2,19', '7,6', '10,49', '1008,031', '1455,06'], ['18/01/2017', '08:05,3', '36,04', '8,697', '2,17', '7,6', '34,131', '1026,495', '1484,05'] ]) # Process the deque data and convert to JSON clean_data = preprocess_marine_data(raw_data_deque, fields) json_output = json.dumps(clean_data, indent=4) # Print or send the JSON output print(json_output)
3. Server-Side Handling Tips
- Batch Processing: If your server receives streaming data into a deque, set up a trigger (e.g., deque reaches 100 rows, or a 1-minute timer) to process and convert batches to JSON instead of handling every row individually.
- Error Logging: Replace the
printstatements with a proper logging system (like Python'sloggingmodule) to track invalid data without crashing your server. - Memory Efficiency: For extremely large datasets, use a generator approach instead of building a full list of processed entries to avoid consuming too much RAM.
内容的提问来源于stack exchange,提问作者Ahmed kharroubi
相关产品推荐
相关产品推荐

