File size: 1,060 Bytes
711f785
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
"""CSV document processor"""

import pandas as pd
import logging
from typing import Dict, Any

logger = logging.getLogger(__name__)


class CSVProcessor:
    """Processor for CSV documents"""

    @staticmethod
    def extract_text(file_path: str) -> str:
        """Extract text from CSV file"""
        try:
            df = pd.read_csv(file_path)
            text = df.to_string()
            logger.info(f"Extracted text from CSV: {len(df)} rows")
            return text
        except Exception as e:
            logger.error(f"Failed to extract text from CSV: {e}")
            raise

    @staticmethod
    def get_metadata(file_path: str) -> Dict[str, Any]:
        """Extract metadata from CSV"""
        try:
            df = pd.read_csv(file_path)
            return {
                "num_rows": len(df),
                "num_columns": len(df.columns),
                "columns": list(df.columns),
            }
        except Exception as e:
            logger.error(f"Failed to extract metadata from CSV: {e}")
            return {"num_rows": 0}