Skip to content

Csv reader

gridgulp.readers.csv_reader

CSV file reader with intelligent parsing and encoding detection.

CSVReader

CSVReader(file_path: Path, file_info: FileInfo)

Bases: SyncBaseReader

Reader for CSV and TSV files with intelligent parsing.

Initialize CSV reader.

Parameters:

  • file_path (Path) –

    Path to CSV file

  • file_info (FileInfo) –

    File information

Source code in src/gridgulp/readers/csv_reader.py
def __init__(self, file_path: Path, file_info: FileInfo):
    """Initialize CSV reader.

    Args:
        file_path: Path to CSV file
        file_info: File information
    """
    super().__init__(file_path, file_info)
    self._encoding: str | None = None
    self._dialect: csv.Dialect | None = None

can_read

can_read() -> bool

Check if can read CSV files.

Returns

bool True if the file type is CSV or TSV; False otherwise.

Notes

This method validates that the CSVReader can handle the given file type. Text files (.txt) are handled by TextReader instead, even if they contain delimited data.

Source code in src/gridgulp/readers/csv_reader.py
def can_read(self) -> bool:
    """Check if can read CSV files.

    Returns
    -------
    bool
        True if the file type is CSV or TSV; False otherwise.

    Notes
    -----
    This method validates that the CSVReader can handle the given file type.
    Text files (.txt) are handled by TextReader instead, even if they
    contain delimited data.
    """
    return self.file_info.type in {FileType.CSV, FileType.TSV}

get_supported_formats

get_supported_formats() -> list[str]

Get supported CSV formats.

Returns

list[str] List of supported file extensions: ["csv", "tsv", "txt"].

Notes

While this method returns "txt" as supported, the actual can_read() method only accepts CSV and TSV file types. Text files are included here for completeness but are typically handled by TextReader.

Source code in src/gridgulp/readers/csv_reader.py
def get_supported_formats(self) -> list[str]:
    """Get supported CSV formats.

    Returns
    -------
    list[str]
        List of supported file extensions: ["csv", "tsv", "txt"].

    Notes
    -----
    While this method returns "txt" as supported, the actual can_read()
    method only accepts CSV and TSV file types. Text files are included
    here for completeness but are typically handled by TextReader.
    """
    return ["csv", "tsv", "txt"]

read_sync

read_sync() -> FileData

Read CSV file synchronously.

Returns:

  • FileData

    FileData with single sheet containing CSV data

Raises:

Source code in src/gridgulp/readers/csv_reader.py
def read_sync(self) -> FileData:
    """Read CSV file synchronously.

    Returns:
        FileData with single sheet containing CSV data

    Raises:
        ReaderError: If file cannot be read
    """
    self._log_read_start()
    self.validate_file()

    try:
        # Detect encoding and dialect
        self._detect_format()

        # Read CSV data
        sheet_data = self._read_csv_data()

        # Create file data with single sheet
        file_data = FileData(
            sheets=[sheet_data],
            metadata={
                "encoding": self._encoding,
                "dialect": (
                    {
                        "delimiter": self._dialect.delimiter,
                        "quotechar": self._dialect.quotechar,
                        "quoting": self._dialect.quoting,
                        "skipinitialspace": self._dialect.skipinitialspace,
                        "lineterminator": repr(self._dialect.lineterminator),
                    }
                    if self._dialect
                    else None
                ),
            },
            file_format=self.file_info.type.value,
        )

        self._log_read_complete(file_data)
        return file_data

    except Exception as e:
        raise ReaderError(f"Failed to read CSV file: {e}") from e