Spaces:
Sleeping
Sleeping
| import csv | |
| import io | |
| _VALID_DIFFICULTIES = {"easy", "medium", "hard"} | |
| def parse_csv_questions(text: str) -> list[dict]: | |
| """ | |
| Parse CSV text into question dicts. | |
| Expected columns: question_text, difficulty | |
| Returns list of {"question_text": str, "difficulty": str}. | |
| Skips rows where question_text is empty. | |
| Defaults difficulty to 'medium' if missing or invalid. | |
| """ | |
| reader = csv.DictReader(io.StringIO(text)) | |
| rows = [] | |
| for row in reader: | |
| question_text = (row.get("question_text") or "").strip() | |
| difficulty = (row.get("difficulty") or "medium").strip().lower() | |
| if not question_text: | |
| continue | |
| if difficulty not in _VALID_DIFFICULTIES: | |
| difficulty = "medium" | |
| rows.append({"question_text": question_text, "difficulty": difficulty}) | |
| return rows | |