-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprocess_mooc.py
More file actions
113 lines (93 loc) · 3.8 KB
/
Copy pathprocess_mooc.py
File metadata and controls
113 lines (93 loc) · 3.8 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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
import json
import sys
import csv
import os
def process_json_to_csv(json_str, output_file="exam_results.csv"):
try:
data = json.loads(json_str)
result = data.get("result", {})
# 1. 建立题目和选项的映射字典
# { qid: { "title": "...", "type": "...", "options": {opt_id: content} } }
question_map = {}
type_desc = {1: "单选", 2: "多选", 4: "判断"}
for q in result.get("objectiveQList", []):
qid = q["id"]
q_type = type_desc.get(q["type"], f"未知({q['type']})")
title = q["plainTextTitle"].strip()
opts = {}
opt_id_to_letter = {}
if q.get("optionDtos"):
for idx, opt in enumerate(q["optionDtos"]):
opts[opt["id"]] = opt["content"].replace("<p>", "").replace("</p>", "").strip()
opt_id_to_letter[opt["id"]] = chr(ord('A') + idx)
question_map[qid] = {
"title": title,
"type": q_type,
"options": opts,
"score": q.get("score", 0),
"opt_id_to_letter": opt_id_to_letter
}
# 2. 解析我的答案并准备写入行
rows = []
my_total_score = result.get("objectiveScore", 0)
potential_total_score = sum(q.get("score", 0) for q in result.get("objectiveQList", []))
score_diff = potential_total_score - my_total_score
for ans in result.get("answers", []):
qid = ans["qid"]
if qid not in question_map:
continue
q_info = question_map[qid]
# 获取所有选项原文(最多4个)
opt_contents = list(q_info["options"].values())
# 补齐4列选项
while len(opt_contents) < 4:
opt_contents.append("")
# 获取我的选项原文
my_opts = [q_info["options"].get(oid, "未知ID") for oid in ans.get("optIds", [])]
my_ans_str = "|".join(my_opts)
# 获取我的选项字母
my_letters = [q_info["opt_id_to_letter"].get(oid, "?") for oid in ans.get("optIds", [])]
my_letters_str = "".join(my_letters)
rows.append([
q_info["title"],
q_info["type"],
opt_contents[0],
opt_contents[1],
opt_contents[2],
opt_contents[3],
my_ans_str,
my_letters_str
])
# 3. 写入 CSV (追加模式)
file_exists = os.path.isfile(output_file)
with open(output_file, 'a', newline='', encoding='utf-8-sig') as f:
writer = csv.writer(f)
# 如果是新文件,写入表头
if not file_exists:
writer.writerow(["题目", "类型", "选项1", "选项2", "选项3", "选项4", "我的选项", "选择"])
writer.writerows(rows)
# 最后追加一行分数差
writer.writerow([f"测验分数差:{score_diff}", "", "", "", "", "", "", ""])
print(f"成功转换并追加到 {output_file}")
except Exception as e:
print(f"解析错误: {e}")
def main():
print("请输入JSON数据(连续两个空行结束输入):")
content = ""
empty_line_count = 0
while True:
line = sys.stdin.readline()
if not line:
break
if line.strip() == "":
empty_line_count += 1
else:
empty_line_count = 0
content += line
if empty_line_count >= 1:
break
if content.strip():
process_json_to_csv(content)
if __name__ == "__main__":
while True:
main()