# CSVLoader

> **Class** in `langchain_community`

📖 [View in docs](https://reference.langchain.com/python/langchain-community/document_loaders/csv_loader/CSVLoader)

Load a `CSV` file into a list of `Document` objects.

Each document represents one row of the CSV file. Every row is converted
into a key/value pair and outputted to a new line in the document's
page_content.

The source for each document loaded from csv is set to the value of the
`file_path` argument for all documents by default.
You can override this by setting the `source_column` argument to the
name of a column in the CSV file.
The source of each document will then be set to the value of the column
with the name specified in `source_column`.

## Signature

```python
CSVLoader(
    self,
    file_path: Union[str, Path],
    source_column: Optional[str] = None,
    metadata_columns: Sequence[str] = (),
    csv_args: Optional[Dict] = None,
    encoding: Optional[str] = None,
    autodetect_encoding: bool = False,
    *,
    content_columns: Sequence[str] = (),
)
```

## Description

**Output Example:**

.. code-block:: txt

column1: value1
column2: value2
column3: value3

**Instantiate:**

.. code-block:: python

from langchain_community.document_loaders import CSVLoader

loader = CSVLoader(file_path='./hw_200.csv',
    csv_args={
    'delimiter': ',',
    'quotechar': '"',
    'fieldnames': ['Index', 'Height', 'Weight']
})

**Load:**

.. code-block:: python

    docs = loader.load()
    print(docs[0].page_content[:100])
    print(docs[0].metadata)

.. code-block:: python

    Index: Index
    Height: Height(Inches)"
    Weight: "Weight(Pounds)"
    {'source': './hw_200.csv', 'row': 0}

**Async load:**

.. code-block:: python

    docs = await loader.aload()
    print(docs[0].page_content[:100])
    print(docs[0].metadata)

.. code-block:: python

    Index: Index
    Height: Height(Inches)"
    Weight: "Weight(Pounds)"
    {'source': './hw_200.csv', 'row': 0}

**Lazy load:**

.. code-block:: python

    docs = []
    docs_lazy = loader.lazy_load()

    # async variant:
    # docs_lazy = await loader.alazy_load()

    for doc in docs_lazy:
        docs.append(doc)
    print(docs[0].page_content[:100])
    print(docs[0].metadata)

.. code-block:: python

    Index: Index
    Height: Height(Inches)"
    Weight: "Weight(Pounds)"
    {'source': './hw_200.csv', 'row': 0}

## Parameters

| Name | Type | Required | Description |
|------|------|----------|-------------|
| `file_path` | `Union[str, Path]` | Yes | The path to the CSV file. |
| `source_column` | `Optional[str]` | No | The name of the column in the CSV file to use as the source. Optional. Defaults to None. (default: `None`) |
| `metadata_columns` | `Sequence[str]` | No | A sequence of column names to use as metadata. Optional. (default: `()`) |
| `csv_args` | `Optional[Dict]` | No | A dictionary of arguments to pass to the csv.DictReader. Optional. Defaults to None. (default: `None`) |
| `encoding` | `Optional[str]` | No | The encoding of the CSV file. Optional. Defaults to None. (default: `None`) |
| `autodetect_encoding` | `bool` | No | Whether to try to autodetect the file encoding. (default: `False`) |
| `content_columns` | `Sequence[str]` | No | A sequence of column names to use for the document content. If not present, use all columns that are not part of the metadata. (default: `()`) |

## Extends

- `BaseLoader`

## Constructors

```python
__init__(
    self,
    file_path: Union[str, Path],
    source_column: Optional[str] = None,
    metadata_columns: Sequence[str] = (),
    csv_args: Optional[Dict] = None,
    encoding: Optional[str] = None,
    autodetect_encoding: bool = False,
    *,
    content_columns: Sequence[str] = (),
)
```

| Name | Type |
|------|------|
| `file_path` | `Union[str, Path]` |
| `source_column` | `Optional[str]` |
| `metadata_columns` | `Sequence[str]` |
| `csv_args` | `Optional[Dict]` |
| `encoding` | `Optional[str]` |
| `autodetect_encoding` | `bool` |
| `content_columns` | `Sequence[str]` |


## Properties

- `file_path`
- `source_column`
- `metadata_columns`
- `encoding`
- `csv_args`
- `autodetect_encoding`
- `content_columns`

## Methods

- [`lazy_load()`](https://reference.langchain.com/python/langchain-community/document_loaders/csv_loader/CSVLoader/lazy_load)

---

[View source on GitHub](https://github.com/langchain-ai/langchain-community/blob/a6a6079511ac8a5c1293337f88096b8641562e77/libs/community/langchain_community/document_loaders/csv_loader.py#L16)