diff --git a/langchain/document_loaders/text.py b/langchain/document_loaders/text.py index 0284de88614..ce7913d6d4e 100644 --- a/langchain/document_loaders/text.py +++ b/langchain/document_loaders/text.py @@ -1,5 +1,4 @@ -"""Load text files.""" -from typing import List +from typing import List, Optional from langchain.docstore.document import Document from langchain.document_loaders.base import BaseLoader @@ -8,13 +7,14 @@ from langchain.document_loaders.base import BaseLoader class TextLoader(BaseLoader): """Load text files.""" - def __init__(self, file_path: str): + def __init__(self, file_path: str, encoding: Optional[str] = None): """Initialize with file path.""" self.file_path = file_path + self.encoding = encoding def load(self) -> List[Document]: """Load from file path.""" - with open(self.file_path, encoding="utf-8") as f: + with open(self.file_path, encoding=self.encoding) as f: text = f.read() metadata = {"source": self.file_path} return [Document(page_content=text, metadata=metadata)]