43 lines
1.3 KiB
Python
43 lines
1.3 KiB
Python
#!/usr/bin/env python3
|
|
"""
|
|
Локальный парсер без Ollama: читает .txt из lists/, парсит табуляцию,
|
|
сохраняет JSON в res/, переносит успешные в done/.
|
|
"""
|
|
|
|
import json
|
|
import pathlib
|
|
import shutil
|
|
|
|
LISTS_DIR = pathlib.Path(__file__).parent / "lists"
|
|
RES_DIR = pathlib.Path(__file__).parent / "res"
|
|
DONE_DIR = pathlib.Path(__file__).parent / "done"
|
|
|
|
|
|
def parse_line(line: str) -> dict:
|
|
parts = line.strip().split("\t")
|
|
return {
|
|
"brand": parts[0] if len(parts) > 0 else "",
|
|
"model": parts[1] if len(parts) > 1 else "",
|
|
"engine_code": parts[2] if len(parts) > 2 else "",
|
|
"date_range": parts[3] if len(parts) > 3 else "",
|
|
}
|
|
|
|
|
|
def main() -> None:
|
|
RES_DIR.mkdir(exist_ok=True)
|
|
DONE_DIR.mkdir(exist_ok=True)
|
|
|
|
for filepath in sorted(LISTS_DIR.glob("*.txt")):
|
|
content = filepath.read_text(encoding="utf-8")
|
|
lines = [ln for ln in content.splitlines() if ln.strip()]
|
|
data = [parse_line(ln) for ln in lines]
|
|
|
|
out_path = RES_DIR / filepath.with_suffix(".json").name
|
|
out_path.write_text(json.dumps(data, ensure_ascii=False, indent=2), encoding="utf-8")
|
|
shutil.move(str(filepath), DONE_DIR / filepath.name)
|
|
print(f" {filepath.name} — {len(data)} записей — OK")
|
|
|
|
|
|
if __name__ == "__main__":
|
|
main()
|