跳至主要内容

如何加载 CSV 数据

一个逗号分隔值 (CSV) 文件是使用逗号分隔值的定界文本文件。文件的每一行都是一个数据记录。每个记录由一个或多个字段组成,这些字段用逗号分隔。

使用每文档一行加载 CSV 数据。

设置

npm install d3-dsv@2

用法,提取所有列

示例 CSV 文件

id,text
1,This is a sentence.
2,This is another sentence.

示例代码

import { CSVLoader } from "@langchain/community/document_loaders/fs/csv";

const loader = new CSVLoader("src/document_loaders/example_data/example.csv");

const docs = await loader.load();
/*
[
Document {
"metadata": {
"line": 1,
"source": "src/document_loaders/example_data/example.csv",
},
"pageContent": "id: 1
text: This is a sentence.",
},
Document {
"metadata": {
"line": 2,
"source": "src/document_loaders/example_data/example.csv",
},
"pageContent": "id: 2
text: This is another sentence.",
},
]
*/

用法,提取单个列

示例 CSV 文件

id,text
1,This is a sentence.
2,This is another sentence.

示例代码

import { CSVLoader } from "@langchain/community/document_loaders/fs/csv";

const loader = new CSVLoader(
"src/document_loaders/example_data/example.csv",
"text"
);

const docs = await loader.load();
/*
[
Document {
"metadata": {
"line": 1,
"source": "src/document_loaders/example_data/example.csv",
},
"pageContent": "This is a sentence.",
},
Document {
"metadata": {
"line": 2,
"source": "src/document_loaders/example_data/example.csv",
},
"pageContent": "This is another sentence.",
},
]
*/

此页面对您有帮助吗?


您也可以在 GitHub 上留下详细的反馈 在 GitHub 上.