feat(csv_import): Enhance CSV import functionality with new endpoint and parsing improvements
- Updated version for csv_import to 0.2.0, reflecting new features. - Implemented a new POST endpoint for universal CSV import, supporting nutrition, weight, and blood pressure modules. - Added CSV parsing function to yield rows as dictionaries for easier data handling. - Enhanced error handling and logging for import operations. - Introduced tests for the new CSV parsing functionality to ensure reliability.
This commit is contained in:
1 parent
36417bfdf3
commit
851018b3b9
6 files changed
+637
-4
No files matched your search
@@ -1,12 +1,13 @@
|
||||
"""
|
||||
CSV bytes → text, delimiter sniffing, strukturierte Erstzeilen für Analyse (Issue #21).
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import csv
|
||||
import io
|
||||
import re
|
||||
from typing import Any, List, Tuple
|
||||
from typing import Any, Dict, Iterator, List, Tuple
|
||||
|
||||
_DEFAULT_DELIMS = [",", ";", "\t"]
|
||||
|
||||
@@ -135,3 +136,22 @@ def get_csv_import_limits(conn_row: dict | None) -> dict[str, int]:
|
||||
out = {**defaults, **{k: int(v) for k, v in val.items() if k in defaults}}
|
||||
return out
|
||||
return defaults
|
||||
|
||||
|
||||
def iter_csv_dict_rows(
|
||||
text: str,
|
||||
delimiter: str,
|
||||
*,
|
||||
has_header: bool = True,
|
||||
) -> Iterator[Dict[str, str]]:
|
||||
"""Vollständige Datei zeilenweise als Dict (Header = Keys)."""
|
||||
if not has_header:
|
||||
raise ValueError("CSV ohne Kopfzeile wird für Import noch nicht unterstützt")
|
||||
normalized = text.replace("\r\n", "\n").replace("\r", "\n")
|
||||
reader = csv.DictReader(io.StringIO(normalized), delimiter=delimiter)
|
||||
for row in reader:
|
||||
if row is None:
|
||||
continue
|
||||
if not any(v and str(v).strip() for v in row.values()):
|
||||
continue
|
||||
yield {k: (v or "").strip() for k, v in row.items()}
|
||||
Reference in new issue
Block a user