From 75c731c78880c5a31a50e166d33dcae03b76349a Mon Sep 17 00:00:00 2001 From: Jan Tuomi Date: Thu, 19 Nov 2020 22:33:18 +0200 Subject: Initial commit --- attain_vocab_to_anki_py/io.py | 38 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) create mode 100644 attain_vocab_to_anki_py/io.py (limited to 'attain_vocab_to_anki_py/io.py') diff --git a/attain_vocab_to_anki_py/io.py b/attain_vocab_to_anki_py/io.py new file mode 100644 index 0000000..4288e06 --- /dev/null +++ b/attain_vocab_to_anki_py/io.py @@ -0,0 +1,38 @@ +from os import path +from dataclasses import dataclass + +@dataclass +class Entry: + kanji: str + hiragana: str + english: str + meanings: str + +def read_from_file(path_str: str) -> list: + p = path.join(path_str) + lines = [] + + with open(p, "r") as f: + lines = f.readlines() + + entries = [] + for line in lines: + kanji, hiragana, english = line.split(",", 2) + entries.append(Entry(kanji=kanji.strip(), + hiragana=hiragana.strip(), + english=english.strip(), + meanings="")) + + return entries + +def _entry_to_row(entry: Entry, delimiter: str = ";") -> str: + s = delimiter.join([entry.kanji, entry.hiragana, entry.english, entry.meanings]) + return f"{s}\n" + +def write_to_file(path_str: str, entries: list): + p = path.join(path_str) + + lines = map(_entry_to_row, entries) + + with open(p, "w") as f: + f.writelines(lines) -- cgit v1.3