Loading CSV and Excel files lets you bring data from spreadsheets into your program easily. This helps you work with real-world data like lists, tables, or reports.
0
0
Loading CSV and Excel files in LangChain
Introduction
You have a spreadsheet with customer information and want to analyze it.
You want to read a CSV file with sales data to create charts.
You need to load an Excel file with multiple sheets for a report.
You want to process data exported from another program in CSV or Excel format.
Syntax
LangChain
from langchain.document_loaders import CSVLoader, ExcelLoader # Load CSV file csv_loader = CSVLoader(file_path="path/to/file.csv") csv_docs = csv_loader.load() # Load Excel file excel_loader = ExcelLoader(file_path="path/to/file.xlsx") excel_docs = excel_loader.load()
Use CSVLoader for CSV files and ExcelLoader for Excel files.
The load() method reads the file and returns documents you can use in LangChain.
Examples
This loads a CSV file named
customers.csv from the data folder.LangChain
from langchain.document_loaders import CSVLoader csv_loader = CSVLoader(file_path="data/customers.csv") docs = csv_loader.load()
This loads an Excel file named
sales.xlsx from the data folder.LangChain
from langchain.document_loaders import ExcelLoader excel_loader = ExcelLoader(file_path="data/sales.xlsx") docs = excel_loader.load()
This loads only the "Summary" sheet from the Excel file.
LangChain
from langchain.document_loaders import ExcelLoader # Load specific sheet by name excel_loader = ExcelLoader(file_path="data/report.xlsx", sheet_name="Summary") docs = excel_loader.load()
Sample Program
This program loads two files: a CSV and an Excel file. It prints how many documents each file contains after loading.
LangChain
from langchain.document_loaders import CSVLoader, ExcelLoader # Load CSV file csv_loader = CSVLoader(file_path="example_data.csv") csv_docs = csv_loader.load() print(f"Loaded {len(csv_docs)} documents from CSV file.") # Load Excel file excel_loader = ExcelLoader(file_path="example_data.xlsx") excel_docs = excel_loader.load() print(f"Loaded {len(excel_docs)} documents from Excel file.")
OutputSuccess
Important Notes
Make sure the file paths are correct and files exist to avoid errors.
Excel files can have multiple sheets; you can specify which sheet to load.
The loaded documents are ready to be used in LangChain workflows like text processing or question answering.
Summary
Use CSVLoader and ExcelLoader to load spreadsheet files easily.
Loading files turns spreadsheet data into documents LangChain can work with.
You can specify sheet names for Excel files to load only needed data.