File size: 503 Bytes
46e547f | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | import json
from pathlib import Path
from typing import Any, Iterable
def _write_jsonl_atomic(rows: Iterable[dict[str, Any]], path: Path) -> int:
path.parent.mkdir(parents=True, exist_ok=True)
temporary = path.with_suffix(path.suffix + ".tmp")
count = 0
with temporary.open("w", encoding="utf-8") as stream:
for row in rows:
stream.write(json.dumps(row, ensure_ascii=False) + "\n")
count += 1
temporary.replace(path)
return count
|