summaryrefslogtreecommitdiffstats
path: root/attain_vocab_to_anki_py/io.py
diff options
context:
space:
mode:
authorJan Tuomi <jan.tuomi@valuemotive.com>2020-11-19 22:33:18 +0200
committerJan Tuomi <jan.tuomi@valuemotive.com>2020-11-19 22:33:18 +0200
commit75c731c78880c5a31a50e166d33dcae03b76349a (patch)
treeeb87160816e3f9a50456f9f924b658cbf5f0602b /attain_vocab_to_anki_py/io.py
Initial commit
Diffstat (limited to 'attain_vocab_to_anki_py/io.py')
-rw-r--r--attain_vocab_to_anki_py/io.py38
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)