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

如何转换格式混乱的JSON数据并提取时间戳生成Pandas可用数据集?

提取嵌套JSON中的时间戳并生成Pandas兼容的CSV

直接用Python就能完成这个任务,以下是具体步骤:

  1. 读取原始JSON文件
    先用标准库的json模块加载你的JSON数据:

    import json
    import pandas as pd
    
    # 替换成你的JSON文件实际路径
    with open('camera_messages.json', 'r', encoding='utf-8') as f:
        raw_data = json.load(f)
    message_list = raw_data['messages']
    
  2. 逐条提取并处理时间戳
    每条消息里的两个时间戳需要分别处理:

    • 外层timestamp是13位毫秒级数值,除以1000转成秒级浮点数(比如1669253760775 → 1669253760.775)
    • payload是转义后的JSON字符串,需要用json.loads解析后,再取出里面的Timestamp字段并转为数值类型

    代码示例(包含异常处理,避免个别脏数据导致程序崩溃):

    processed_records = []
    for msg in message_list:
        try:
            # 处理外层时间戳
            outer_timestamp = msg['timestamp'] / 1000
            # 解析嵌套的payload JSON
            payload_data = json.loads(msg['payload'])
            inner_timestamp = float(payload_data['Timestamp'])
            # 收集处理后的数据
            processed_records.append({
                'outer_timestamp': outer_timestamp,
                'inner_timestamp': inner_timestamp
            })
        except Exception as e:
            print(f"跳过异常消息: {str(e)}")
            continue
    
  3. 生成Pandas可读取的CSV
    用Pandas把处理好的数据转成DataFrame,再导出为CSV:

    df = pd.DataFrame(processed_records)
    # 导出到指定路径,index=False避免生成多余的索引列
    df.to_csv('timestamps_result.csv', index=False)
    

生成的CSV文件可以直接用pd.read_csv('timestamps_result.csv')读取,完全满足时间序列分析的格式需求。如果后续需要提取AnalyticalOutput数组中的数据,只需要在processed_records的字典中添加对应的字段即可。

内容的提问来源于stack exchange,提问作者Johnney

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.11 16:45:32