-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcreate_csv.py
More file actions
46 lines (32 loc) · 1.38 KB
/
Copy pathcreate_csv.py
File metadata and controls
46 lines (32 loc) · 1.38 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
import json
import csv
import re
import os
INPUT_FILE = "cards_response.txt"
with open(INPUT_FILE, "r", encoding="utf-8") as f:
data = json.load(f)
subject_id = data.get("replyContent", {}).get("cards", [{}])[0].get("subjectIdString", "unknown_subject")
txt_file = f"{subject_id}.txt"
csv_file = f"{subject_id}.csv"
with open(txt_file, "w", encoding="utf-8") as f:
json.dump(data, f, ensure_ascii=False, indent=2)
print(f"Response saved: {txt_file}")
audio_regex = re.compile(r"\[\{\~.*?:audio~\}\]")
cards_list = data.get("replyContent", {}).get("cards", [])
with open(csv_file, "w", newline="", encoding="utf-8") as csvfile:
writer = csv.writer(csvfile, delimiter=",", quotechar='"', quoting=csv.QUOTE_MINIMAL)
writer.writerow(["Deutsch", "Spanisch", "Beispiel", "LearningDirection", "UnitID"])
for c in cards_list:
if not isinstance(c, dict):
continue
content = c.get("cardContent")
if not content:
continue
de = content.get("question", "")
es = content.get("answer", "")
example = content.get("answerExample", "")
learning_direction = c.get("learningDirection", "")
unit_id = c.get("unitIdString", "")
es = audio_regex.sub("", es)
writer.writerow([de, es, example, learning_direction, unit_id])
print(f"CSV created: {csv_file}")