Python 逐行读取 JSON 示例

语言:
Python
6 浏览
0 收藏
2小时前

代码实现

Python
import json

def read_jsonl(file_path):
    with open(file_path, "r", encoding="utf-8") as f:
        for line in f:
            yield json.loads(line)

# Usage
for record in read_jsonl("data.jsonl"):
    print(record)

Python逐行读取 JSONL (JSON Lines) 文件,适合处理大规模数据集。

#json#jsonl

片段说明

  • JSONL 文件每行都是一个独立 JSON 对象
  • yield 实现逐行生成器,避免一次性加载全部数据
  • 常用于日志、大数据场景

评论

加载中...