langchain_community.document_loaders.csv_loader.CSVLoader¶

class langchain_community.document_loaders.csv_loader.CSVLoader(file_path: str, source_column: Optional[str] = None, metadata_columns: Sequence[str] = (), csv_args: Optional[Dict] = None, encoding: Optional[str] = None, autodetect_encoding: bool = False)[source]¶

Load a CSV file into a list of Documents.

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.

Output Example:
column1: value1
column2: value2
column3: value3
Parameters
  • file_path (str) – The path to the CSV file.

  • source_column (Optional[str]) – The name of the column in the CSV file to use as the source. Optional. Defaults to None.

  • metadata_columns (Sequence[str]) – A sequence of column names to use as metadata. Optional.

  • csv_args (Optional[Dict]) – A dictionary of arguments to pass to the csv.DictReader. Optional. Defaults to None.

  • encoding (Optional[str]) – The encoding of the CSV file. Optional. Defaults to None.

  • autodetect_encoding (bool) – Whether to try to autodetect the file encoding.

Methods

__init__(file_path[, source_column, ...])

param file_path

The path to the CSV file.

alazy_load()

A lazy loader for Documents.

lazy_load()

A lazy loader for Documents.

load()

Load data into Document objects.

load_and_split([text_splitter])

Load Documents and split into chunks.

__init__(file_path: str, source_column: Optional[str] = None, metadata_columns: Sequence[str] = (), csv_args: Optional[Dict] = None, encoding: Optional[str] = None, autodetect_encoding: bool = False)[source]¶
Parameters
  • file_path (str) – The path to the CSV file.

  • source_column (Optional[str]) – The name of the column in the CSV file to use as the source. Optional. Defaults to None.

  • metadata_columns (Sequence[str]) – A sequence of column names to use as metadata. Optional.

  • csv_args (Optional[Dict]) – A dictionary of arguments to pass to the csv.DictReader. Optional. Defaults to None.

  • encoding (Optional[str]) – The encoding of the CSV file. Optional. Defaults to None.

  • autodetect_encoding (bool) – Whether to try to autodetect the file encoding.

async alazy_load() AsyncIterator[Document]¶

A lazy loader for Documents.

Return type

AsyncIterator[Document]

lazy_load() Iterator[Document][source]¶

A lazy loader for Documents.

Return type

Iterator[Document]

load() List[Document]¶

Load data into Document objects.

Return type

List[Document]

load_and_split(text_splitter: Optional[TextSplitter] = None) List[Document]¶

Load Documents and split into chunks. Chunks are returned as Documents.

Do not override this method. It should be considered to be deprecated!

Parameters

text_splitter (Optional[TextSplitter]) – TextSplitter instance to use for splitting documents. Defaults to RecursiveCharacterTextSplitter.

Returns

List of Documents.

Return type

List[Document]

Examples using CSVLoader¶