diff options
Diffstat (limited to 'attain_vocab_to_anki_py/io.py')
| -rw-r--r-- | attain_vocab_to_anki_py/io.py | 38 |
1 files changed, 38 insertions, 0 deletions
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) |
