如何加载 CSV 数据
一个 逗号分隔值 (CSV) 文件是一个分隔文本文件,它使用逗号分隔值。文件中的每一行都是一条数据记录。每条记录包含一个或多个字段,这些字段由逗号分隔。
加载 CSV 数据,每个文档一行。
设置
- npm
- Yarn
- pnpm
npm install d3-dsv@2
yarn add d3-dsv@2
pnpm add 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.",
},
]
*/