Skip to content

Latest commit

 

History

History
75 lines (57 loc) · 2.48 KB

File metadata and controls

75 lines (57 loc) · 2.48 KB
title CSVToDocument
id csvtodocument
slug /csvtodocument
description Converts CSV files to documents.

CSVToDocument

Converts CSV files to documents.

Most common position in a pipeline Before PreProcessors , or right at the beginning of an indexing pipeline
Mandatory run variables sources: A list of file paths or ByteStream objects
Output variables documents: A list of documents
API reference Converters
GitHub link https://github.com/deepset-ai/haystack/blob/main/haystack/components/converters/csv.py
Package name haystack-ai

Overview

CSVToDocument converts one or more CSV files into a text document.

The component uses UTF-8 encoding by default, but you may specify a different encoding if needed during initialization. You can optionally attach metadata to each document with a meta parameter when running the component.

Usage

On its own

from haystack.components.converters.csv import CSVToDocument

converter = CSVToDocument()
results = converter.run(
    sources=["sample.csv"],
    meta={"date_added": datetime.now().isoformat()},
)
documents = results["documents"]

print(documents[0].content)
## 'col1,col2\now1,row1\nrow2row2\n'

In a pipeline

from haystack import Pipeline
from haystack.document_stores.in_memory import InMemoryDocumentStore
from haystack.components.converters import CSVToDocument
from haystack.components.preprocessors import DocumentCleaner
from haystack.components.preprocessors import DocumentSplitter
from haystack.components.writers import DocumentWriter

document_store = InMemoryDocumentStore()

pipeline = Pipeline()
pipeline.add_component("converter", CSVToDocument())
pipeline.add_component("cleaner", DocumentCleaner())
pipeline.add_component(
    "splitter",
    DocumentSplitter(split_by="sentence", split_length=5),
)
pipeline.add_component("writer", DocumentWriter(document_store=document_store))
pipeline.connect("converter", "cleaner")
pipeline.connect("cleaner", "splitter")
pipeline.connect("splitter", "writer")

pipeline.run({"converter": {"sources": file_names}})