ai
  • index
  • cursor
  • vector
  • crawl
  • crawl-front
  • DrissionPage
  • logging
  • mysql
  • pprint
  • sqlalchemy
  • contextmanager
  • dotenv
  • Flask
  • python
  • job
  • pdfplumber
  • python-docx
  • redbook
  • douyin
  • ffmpeg
  • json
  • numpy
  • opencv-python
  • pypinyin
  • re
  • requests
  • subprocess
  • time
  • uuid
  • watermark
  • milvus
  • pymilvus
  • search
  • Blueprint
  • flash
  • Jinja2
  • secure_filename
  • url_for
  • Werkzeug
  • chroma
  • HNSW
  • pillow
  • pandas
  • beautifulsoup4
  • langchain-community
  • langchain-core
  • langchain
  • langchain_unstructured
  • libreoffice
  • lxml
  • openpyxl
  • pymupdf
  • python-pptx
  • RAGFlow
  • tabulate
  • sentence_transformers
  • jsonl
  • collections
  • jieba
  • rag_optimize
  • rag
  • rank_bm25
  • Hugging_Face
  • modelscope
  • all-MiniLM-L6-v2
  • ollama
  • rag_measure
  • ragas
  • ASGI
  • FastAPI
  • FastChat
  • Jupyter
  • PyTorch
  • serper
  • uvicorn
  • markdownify
  • NormalizedLevenshtein
  • raq-action
  • CrossEncoder
  • Bi-Encoder
  • neo4j
  • neo4j4python
  • matplotlib
  • Plotly
  • Streamlit
  • py2neo
  • abc
  • read_csv
  • neo4jinstall
  • APOC
  • neo4jproject
  • uv
  • GDS
  • heapq
  • 什么是JSONL格式?
  • JSONL示例
  • 为什么要使用JSONL?
  • 如何读取JSONL文件
    • 在Python中读取JSONL
    • 在JavaScript中读取JSONL
    • 在命令行中处理JSONL
  • JSONL与JSON的区别

什么是JSONL格式? #

JSONL(JSON Lines)是一种简单的文本格式,每行都是一个有效的JSON对象。与普通的JSON数组不同,JSONL格式具有以下特点:

  • 每行一个独立的JSON对象
  • 行与行之间用换行符(\n)分隔
  • 不需要外层的数组括号
  • 不需要行间的逗号分隔符

JSONL示例 #

{"name": "Alice", "age": 25, "city": "New York"}
{"name": "Bob", "age": 30, "city": "Chicago"}
{"name": "Charlie", "age": 35, "city": "Los Angeles"}

为什么要使用JSONL? #

  1. 流式处理友好:可以逐行读取和处理,不需要加载整个文件
  2. 内存效率高:适合处理大型数据集
  3. 追加数据方便:只需在文件末尾添加新行
  4. 错误恢复能力强:某行格式错误不会影响其他行

如何读取JSONL文件 #

在Python中读取JSONL #

import json

# 方法1:逐行读取
with open('data.jsonl', 'r') as f:
    for line in f:
        data = json.loads(line)
        print(data)

# 方法2:使用第三方库(如jsonlines)
import jsonlines

with jsonlines.open('data.jsonl') as reader:
    for obj in reader:
        print(obj)

在JavaScript中读取JSONL #

const fs = require('fs');
const readline = require('readline');

async function processJSONL(filePath) {
  const fileStream = fs.createReadStream(filePath);
  const rl = readline.createInterface({
    input: fileStream,
    crlfDelay: Infinity
  });

  for await (const line of rl) {
    const data = JSON.parse(line);
    console.log(data);
  }
}

processJSONL('data.jsonl');

在命令行中处理JSONL #

# 查看前几行
head -n 5 data.jsonl

# 使用jq工具处理
cat data.jsonl | jq '.name'

# 统计行数(对象数)
wc -l data.jsonl

JSONL与JSON的区别 #

特性 JSON JSONL
格式 单个数组/对象 多行独立对象
文件大小 适合小数据 适合大数据
内存使用 需要全部加载 可逐行处理
可读性 结构清晰 每行独立
错误处理 全部或没有 单行错误不影响其他

访问验证

请输入访问令牌

Token不正确,请重新输入