● Zion Boggan
repos/Sightline/backend/app/services/parsers/csv_flock.py
zionboggan.com ↗
29 lines · python
History for this file →
1
from __future__ import annotations
2
 
3
import csv
4
import io
5
 
6
from app.services.normalize import RawRead
7
from app.services.parsers.base import BaseParser, ParseResult
8
 
9
 
10
class CsvFlockParser(BaseParser):
11
    name = "csv_flock"
12
 
13
    def parse(self, data: bytes) -> ParseResult:
14
        text = data.decode("utf-8-sig", errors="replace")
15
        sample = text[:4096]
16
        try:
17
            dialect = csv.Sniffer().sniff(sample, delimiters=",;\t|")
18
        except csv.Error:
19
            dialect = csv.excel
20
        reader = csv.DictReader(io.StringIO(text), dialect=dialect)
21
        headers = reader.fieldnames or []
22
        rows: list[RawRead] = []
23
        errors: list[str] = []
24
        for i, row in enumerate(reader, start=1):
25
            clean = {k: v for k, v in row.items() if k is not None}
26
            if not any((v or "").strip() for v in clean.values()):
27
                continue
28
            rows.append(RawRead(fields=clean, source_row=i))
29
        return ParseResult(headers=list(headers), rows=rows, errors=errors)