> ## Documentation Index
> Fetch the complete documentation index at: https://langchain-zh.cn/llms.txt
> Use this file to discover all available pages before exploring further.

# CSV 集成

> 使用 LangChain JavaScript 集成 CSV 文档加载器。

<Tip>
  **兼容性**：仅适用于 Node.js。
</Tip>

本笔记本提供了快速入门 `CSVLoader` [文档加载器](/oss/javascript/integrations/document_loaders) 的概述。有关 `CSVLoader` 所有功能和配置的详细文档，请参阅 [API 参考](https://reference.langchain.com/javascript/langchain-community/document_loaders/fs/csv/CSVLoader)。

本示例介绍了如何从 CSV 文件加载数据。第二个参数是要从 CSV 文件中提取的 `column` 列名。CSV 文件中的每一行将创建一个文档。当未指定 `column` 时，每一行将转换为键/值对，每个键/值对输出到文档 `pageContent` 的新行中。当指定 `column` 时，每一行创建一个文档，并使用指定列的值作为文档的 `pageContent`。

## 概述

### 集成详情

| 类                                                                                                               | 包                                                                            |   兼容性  |  本地 | [Python 支持](https://python.langchain.com/docs/integrations/document_loaders/csv) |
| :-------------------------------------------------------------------------------------------------------------- | :--------------------------------------------------------------------------- | :----: | :-: | :------------------------------------------------------------------------------: |
| [`CSVLoader`](https://reference.langchain.com/javascript/langchain-community/document_loaders/fs/csv/CSVLoader) | [`@langchain/community`](https://www.npmjs.com/package/@langchain/community) | 仅 Node |  ✅  |                                         ✅                                        |

## 设置

要使用 `CSVLoader` 文档加载器，您需要安装 `@langchain/community` 集成包以及 `d3-dsv@2` 对等依赖项。

### 安装

LangChain CSVLoader 集成位于 `@langchain/community` 集成包中。

<CodeGroup>
  ```bash npm theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
  npm install @langchain/community @langchain/core d3-dsv@2
  ```

  ```bash yarn theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
  yarn add @langchain/community @langchain/core d3-dsv@2
  ```

  ```bash pnpm theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
  pnpm add @langchain/community @langchain/core d3-dsv@2
  ```
</CodeGroup>

## 实例化

现在我们可以实例化模型对象并加载文档：

```typescript theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
import { CSVLoader } from "@langchain/community/document_loaders/fs/csv"

const exampleCsvPath = "../../../../../../langchain/src/document_loaders/tests/example_data/example_separator.csv";

const loader = new CSVLoader(exampleCsvPath)
```

## 加载

```typescript theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
const docs = await loader.load()
docs[0]
```

```javascript theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
Document {
  pageContent: 'id｜html: 1｜"<i>Corruption discovered at the core of the Banking Clan!</i>"',
  metadata: {
    source: '../../../../../../langchain/src/document_loaders/tests/example_data/example_separator.csv',
    line: 1
  },
  id: undefined
}
```

```typescript theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
console.log(docs[0].metadata)
```

```javascript theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
{
  source: '../../../../../../langchain/src/document_loaders/tests/example_data/example_separator.csv',
  line: 1
}
```

## 用法：提取单列

示例 CSV 文件：

```csv theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
id｜html
1｜"<i>Corruption discovered at the core of the Banking Clan!</i>"
2｜"<i>Reunited, Rush Clovis and Senator Amidala</i>"
3｜"<i>discover the full extent of the deception.</i>"
4｜"<i>Anakin Skywalker is sent to the rescue!</i>"
```

```typescript theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
import { CSVLoader } from "@langchain/community/document_loaders/fs/csv";

const singleColumnLoader = new CSVLoader(
  exampleCsvPath,
  {
    column: "html",
    separator:"｜"
  }
);

const singleColumnDocs = await singleColumnLoader.load();
console.log(singleColumnDocs[0]);
```

```javascript theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
Document {
  pageContent: '<i>Corruption discovered at the core of the Banking Clan!</i>',
  metadata: {
    source: '../../../../../../langchain/src/document_loaders/tests/example_data/example_separator.csv',
    line: 1
  },
  id: undefined
}
```

***

## API 参考

有关 CSVLoader 所有功能和配置的详细文档，请参阅 [API 参考](https://reference.langchain.com/javascript/langchain-community/document_loaders/fs/csv/CSVLoader)。

***

<div className="source-links">
  <Callout icon="edit">
    [Edit this page on GitHub](https://github.com/langchain-ai/docs/edit/main/src/i18n\zh-CN\oss\javascript\integrations\document_loaders\file_loaders\csv.mdx) or [file an issue](https://github.com/langchain-ai/docs/issues/new/choose).
  </Callout>

  <Callout icon="terminal-2">
    [Connect these docs](/use-these-docs) to Claude, VSCode, and more via MCP for real-time answers.
  </Callout>
</div>
