40 lines
991 B
Python
40 lines
991 B
Python
from __future__ import annotations
|
|
|
|
import csv
|
|
from pathlib import Path
|
|
|
|
from edkb_system.contracts import Context
|
|
|
|
|
|
def run(input_data: dict, context: Context) -> dict:
|
|
file_path = Path(input_data["file_path"])
|
|
raw = None
|
|
for enc in ("utf-8-sig", "utf-8", "gb18030"):
|
|
try:
|
|
raw = file_path.read_text(encoding=enc)
|
|
break
|
|
except UnicodeDecodeError:
|
|
continue
|
|
if raw is None:
|
|
raw = file_path.read_text(errors="ignore")
|
|
|
|
sample = raw[:4096]
|
|
try:
|
|
dialect = csv.Sniffer().sniff(sample, delimiters=",;\t")
|
|
delim = dialect.delimiter
|
|
except Exception:
|
|
delim = ","
|
|
|
|
rows = [
|
|
r
|
|
for r in csv.reader(raw.splitlines(), delimiter=delim)
|
|
if any(c.strip() for c in r)
|
|
]
|
|
table = {
|
|
"sheet": "csv",
|
|
"headers": rows[0] if rows else [],
|
|
"rows": rows[1:] if len(rows) > 1 else [],
|
|
}
|
|
context.extras["tables_count"] = 1
|
|
return table
|