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

# CSV 集成

> 使用 LangChain JavaScript 集成 CSV document loader。

<Tip>
  **Compatibility**: Only available on Node.js.
</Tip>

This notebook provides a quick overview for getting started with `CSVLoader` [document loaders](/oss/javascript/integrations/document_loaders). For detailed documentation of all `CSVLoader` features and configurations head to the [API reference](https://reference.langchain.com/javascript/langchain-community/document_loaders/fs/csv/CSVLoader).

This example goes over how to load data from CSV files. The second argument is the `column` name to extract from the CSV file. One document will be created for each row in the CSV file. When `column` is not specified, each row is converted into a key/value pair with each key/value pair outputted to a new line in the document's `pageContent`. When `column` is specified, one document is created for each row, and the value of the specified column is used as the document's `pageContent`.

## 概述

### 集成详情

| Class                                                                                                           | Package                                                                      | Compatibility | Local | [PY support](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-only   |   ✅   |                                         ✅                                         |

## 设置

要访问 `CSVLoader` document loader，你需要install the `@langchain/community` integration, along with the `d3-dsv@2` peer dependency.

### 安装

The LangChain CSVLoader integration lives in the `@langchain/community` integration package.

<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>

## 实例化

Now we can instantiate our model object and load documents:

```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)
```

## Load

```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
}
```

## Usage, extracting a single column

Example CSV file:

```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="terminal-2">
    [将这些文档连接到](/use-these-docs) Claude、VSCode 等工具，通过 MCP 获取实时答案。
  </Callout>

  <Callout icon="edit">
    [在 GitHub 上编辑此页面](https://github.com/langchain-ai/docs/edit/main/src/oss/javascript/integrations/document_loaders/file_loaders/csv.mdx) 或 [提交 issue](https://github.com/langchain-ai/docs/issues/new/choose)。
  </Callout>
</div>
