CSV Loader

Open in ColabOpen in GitHub

Overview

This tutorial provides a comprehensive guide on how to use the CSVLoader utility in LangChain to seamlessly integrate data from CSV files into your applications. The CSVLoader is a powerful tool for processing structured data, enabling developers to extract, parse, and utilize information from CSV files within the LangChain framework.

Comma-Separated Values (CSV) is one of the most common formats for storing and exchanging data.

CSVLoader simplifies the process of loading, parsing, and extracting data from CSV files, allowing developers to seamlessly incorporate this information into LangChain workflows.

Table of Contents

References


Environment Setup

Set up the environment. You may refer to Environment Setup for more details.

[Note]

  • langchain-opentutorial is a package that provides a set of easy-to-use environment setup, useful functions and utilities for tutorials.

  • You can check out the langchain-opentutorial for more details.

  • unstructured package is a Python library for extracting text and metadata from various document formats like PDF and CSV

You can alternatively set OPENAI_API_KEY in .env file and load it.

[Note] This is not necessary if you've already set OPENAI_API_KEY in previous steps.

How to load CSVs

A comma-separated values (CSV) file is a delimited text file that uses a comma to separate values. LangChain can help you load CSV files easily—just import CSVLoader to get started.

Each line of the file is a data record, and each record consists of one or more fields, separated by commas.

We use a sample CSV file for the example.

Customizing the CSV parsing and loading

CSVLoader accepts a csv_args keyword argument that supports customization of the parameters passed to Python's csv.DictReader. This allows you to handle various CSV formats, such as custom delimiters, quote characters, or specific newline handling.

See Python's csv module documentation for more information on supported csv_args and how to tailor the parsing to your specific needs.

Specify a column to identify the document source

You should use the source_column argument to specify the source of the documents generated from each row. Otherwise file_path will be used as the source for all documents created from the CSV file.

This is particularly useful when using the documents loaded from a CSV file in a chain designed to answer questions based on their source.

Generating XML document format

This example shows how to generate XML Document format from CSVLoader. By processing data from a CSV file, you can convert its rows and columns into a structured XML representation.

Convert a row in the document.

Convert entire rows in the document.

UnstructuredCSVLoader

UnstructuredCSVLoader can be used in both single and elements mode. If you use the loader in “elements” mode, the CSV file will be a single Unstructured Table element. If you use the loader in elements” mode, an HTML representation of the table will be available in the text_as_html key in the document metadata.

Last updated