Linux教程:日志转CSV到JSON

发布: (2026年1月18日 GMT+8 21:59)
2 min read
原文: Dev.to

Source: Dev.to

设置目录

mkdir -p tutorial
cd tutorial

生成示例日志

# Generate logs inside the tutorial folder
echo "2026-01-18 05:42:09 | INFO | system initialized" >> myapp.log
echo "2026-01-18 05:42:09 | ERROR | disk space low" >> myapp.log
echo "2026-01-18 05:42:09 | INFO | user logged in" >> myapp.log

文件: myapp.log

2026-01-18 05:42:09 | INFO | system initialized
2026-01-18 05:42:09 | ERROR | disk space low
2026-01-18 05:42:09 | INFO | user logged in

将日志转换为 CSV

sed 's/ | /","/g; s/^/"/; s/$/"/' myapp.log > myapp.csv

文件: myapp.csv

"2026-01-18 05:42:09","INFO","system initialized"
"2026-01-18 05:42:09","ERROR","disk space low"
"2026-01-18 05:42:09","INFO","user logged in"

CSV‑转‑JSON Python 脚本

创建 csv_to_json.py 并粘贴以下代码:

import csv
import json
import sys
from pathlib import Path

def convert(csv_file, json_file):
    csv_path = Path(csv_file)
    json_path = Path(json_file)

    if not csv_path.exists():
        print(f"Error: Input file '{csv_file}' not found.")
        print("Make sure you are running this from the project root and have generated the CSV.")
        sys.exit(1)

    data = []
    with open(csv_path, mode='r', encoding='utf-8') as f:
        reader = csv.DictReader(f, fieldnames=["timestamp", "level", "message"])
        for row in reader:
            data.append(row)

    # Ensure parent directory for output exists
    json_path.parent.mkdir(parents=True, exist_ok=True)

    with open(json_path, mode='w', encoding='utf-8') as f:
        json.dump(data, f, indent=4)

    print(f"Successfully converted {csv_file} to {json_file}!")

if __name__ == "__main__":
    # Find files relative to the script's own directory
    script_dir = Path(__file__).parent
    csv_file = script_dir / 'myapp.csv'
    json_file = script_dir / 'myapp.json'

    convert(csv_file, json_file)

运行转换

python3 csv_to_json.py

文件: myapp.json

[
    {
        "timestamp": "2026-01-18 05:42:09",
        "level": "INFO",
        "message": "system initialized"
    },
    {
        "timestamp": "2026-01-18 05:42:09",
        "level": "ERROR",
        "message": "disk space low"
    },
    {
        "timestamp": "2026-01-18 05:42:09",
        "level": "INFO",
        "message": "user logged in"
    }
]

生成的 JSON 文件可供 Web 仪表盘、数据分析工具使用,或用于为语言模型实验生成测试和基准数据。

Back to Blog

相关文章

阅读更多 »

智能

bash !/bin/bash 用法: ./filter.sh file.csv decimals col1 col2 … colN 示例: ./filter.sh data.csv 2 45 if '$' -lt 3 ; then echo 'Usage: $0 file.csv decimals...'