Skip to content

trading_agent.attachments.plain_text_reader

Reads files that already are text: plain text, Markdown, CSV, TSV and JSON.

Typical usage example:

extracted = PlainTextReader().read(Path('trades.csv'))

PlainTextReader

Bases: TextReader

Reads a text file as it is.

read

read(path)

Reads the file, replacing bytes that are not UTF-8.

Parameters:

Name Type Description Default
path Path

The file.

required

Returns:

Type Description
ExtractedText

Its text.

Raises:

Type Description
TextReadError

The file cannot be opened.

Source code in src/trading_agent/attachments/plain_text_reader.py
def read(self, path: Path) -> text_reader.ExtractedText:
    """Reads the file, replacing bytes that are not UTF-8.

    Args:
        path (Path): The file.

    Returns:
        text_reader.ExtractedText: Its text.

    Raises:
        TextReadError: The file cannot be opened.
    """
    try:
        text = path.read_text(encoding='utf-8-sig', errors='replace')
    except OSError as error:
        raise text_reader.TextReadError(
            f'the file cannot be opened ({error})'
        ) from error
    return text_reader.ExtractedText(text=text)