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

Windows EVTX日志转JSON再转Pandas DataFrame遇格式问题求助

问题分析与解决方案

问题出在你获取的record['data']是JSON格式的字符串,而非可直接转换为DataFrame的Python字典。直接将字符串存入列表,Pandas会把每个字符串当作单列数据处理,自然无法解析日志的字段结构。

正确处理步骤

  1. 导入json模块,将每条record['data']解析为Python字典
  2. 将解析后的字典存入列表,再直接转换为DataFrame

修正后的代码

from evtx import PyEvtxParser
import pandas as pd
import json

def main(evtx_file):
    records_list = []
    parser = PyEvtxParser(evtx_file)
    for record in parser.records_json():
        try:
            # 将JSON字符串解析为可被Pandas识别的字典
            record_dict = json.loads(record['data'])
            records_list.append(record_dict)
        except json.JSONDecodeError as e:
            print(f"解析单条日志失败: {e}")
            continue
    # 直接将字典列表转换为结构化DataFrame
    df = pd.DataFrame(records_list)
    return df

# 替换为你的EVTX文件实际路径
evtx_path = "your/log/file.evtx"
df = main(evtx_path)
# 查看前5条数据验证结果
print(df.head())

为什么之前的方法无效

  • 直接用pd.DataFrame(list):列表元素是未解析的JSON字符串,Pandas无法识别内部字段,只能生成单列数据。
  • 用json.dumps(list) + pd.read_json(..., lines=True):json.dumps(list)生成的是完整JSON数组,而lines=True要求输入是每行一个独立JSON对象的格式,两者格式不匹配,导致解析异常并出现警告。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.08 19:12:35