38 lines
1.1 KiB
Python
38 lines
1.1 KiB
Python
"""nsv_io.py — чтение/запись .nsv диалоговых файлов через пакет nsv."""
|
|
|
|
from pathlib import Path
|
|
|
|
import nsv
|
|
|
|
FIELDS = ("line", "speaker", "pcm", "text", "translated_text")
|
|
|
|
|
|
def load_nsv_file(path: Path) -> list[dict]:
|
|
with open(path, "r", encoding="utf-8", newline="") as f:
|
|
it = iter(nsv.load(f))
|
|
next(it, None)
|
|
entries = []
|
|
for line, speaker, pcm, text, translated_text in it:
|
|
entries.append({
|
|
"line": int(line or 0),
|
|
"speaker": speaker or None,
|
|
"pcm": pcm or None,
|
|
"text": text,
|
|
"translated_text": translated_text,
|
|
})
|
|
return entries
|
|
|
|
|
|
def save_nsv_file(path: Path, entries: list[dict]) -> None:
|
|
rows = [list(FIELDS)]
|
|
for e in entries:
|
|
rows.append([
|
|
str(e.get("line", "")),
|
|
e.get("speaker") or "",
|
|
e.get("pcm") or "",
|
|
e.get("text", ""),
|
|
e.get("translated_text", ""),
|
|
])
|
|
with open(path, "w", encoding="utf-8", newline="") as f:
|
|
nsv.dump(rows, f)
|