You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何将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,217 instead of 12.217)
  • Date format is dd/mm/yyyy but your field expects yyyy-mm-dd
  • Time values have commas for milliseconds (e.g., 08:04,3 instead of 08: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 print statements with a proper logging system (like Python's logging module) 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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.26 08:27:19