quiz-the-word/scripts/file_parsing.py
2023-11-27 19:25:33 -08:00

27 lines
928 B
Python

from __future__ import annotations
import pathlib
def parse_multiple_choice_questions(question_file: pathlib.Path):
file_data = question_file.read_text("utf-8").split("\n")
questions = []
for line in file_data:
question_data = line.split("~")
if len(question_data) == 4:
correct_answer_index = int(question_data[0])-1
question = question_data[1]
answer_list = question_data[2].split("//")
bible_verse = question_data[3] if question_data[3].lower(
) != "none" else None
correct_answer = answer_list.pop(correct_answer_index)
questions.append({
"question": question,
"answer": correct_answer,
"addresses": bible_verse,
"difficulty": None,
"hint": None,
"wrong_answers": answer_list,
})
return questions