113 lines
3.5 KiB
Plaintext
113 lines
3.5 KiB
Plaintext
{
|
|
"cells": [
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 1,
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"#修改起始id,出处,文件名\n",
|
|
"starting_id = 11988\n",
|
|
"origin = \"\"\n",
|
|
"filename = \"../临时/test3.tex\""
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 15,
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"import os,re,json\n",
|
|
"\n",
|
|
"#从enumerate环境的字符串生成题目列表\n",
|
|
"def GenerateProblemListFromString(data):\n",
|
|
" try:\n",
|
|
" data = re.findall(r\"\\\\begin\\{document\\}([\\s\\S]*?)\\\\end\\{document\\}\",data)[0]\n",
|
|
" except:\n",
|
|
" pass\n",
|
|
" data = re.sub(r\"\\n[\\s]*?\\%[\\s\\S]*?\\n\",\"\\n\",data)\n",
|
|
" data = re.sub(r\"\\n{2,}\",\"\\n\",data)\n",
|
|
" data = re.sub(r\"\\\\item\",r\"\\\\enditem\\\\item\",data)\n",
|
|
" data = re.sub(r\"\\\\end\\{enumerate\\}\",r\"\\\\enditem\",data)\n",
|
|
" ProblemList = [p.strip() for p in re.findall(r\"\\\\item([\\s\\S]*?)\\\\enditem\",data)]\n",
|
|
" return ProblemList\n",
|
|
"\n",
|
|
"# 创建新的空题目\n",
|
|
"def CreateEmptyProblem(problem):\n",
|
|
" NewProblem = problem.copy()\n",
|
|
" for field in NewProblem:\n",
|
|
" if type(NewProblem[field]) == str:\n",
|
|
" NewProblem[field] = \"\"\n",
|
|
" elif type(NewProblem[field]) == list:\n",
|
|
" NewProblem[field] = []\n",
|
|
" elif type(NewProblem[field]) == int or type(NewProblem[field]) == float:\n",
|
|
" NewProblem[field] = -1\n",
|
|
" return NewProblem\n",
|
|
"\n",
|
|
"# 创建新题目\n",
|
|
"def CreateNewProblem(id,content,origin,dict):\n",
|
|
" NewProblem = CreateEmptyProblem(dict[\"000001\"])\n",
|
|
" NewProblem[\"id\"] = str(id).zfill(6)\n",
|
|
" NewProblem[\"content\"] = content\n",
|
|
" NewProblem[\"origin\"] = origin\n",
|
|
" return NewProblem\n",
|
|
"\n",
|
|
"\n",
|
|
"with open(r\"../题库0.3/Problems.json\",\"r\",encoding = \"utf8\") as f:\n",
|
|
" database = f.read()\n",
|
|
"pro_dict = json.loads(database)\n",
|
|
"\n",
|
|
"with open(filename,\"r\",encoding = \"utf8\") as f:\n",
|
|
" problems_string = f.read()\n",
|
|
"problems = GenerateProblemListFromString(problems_string)\n",
|
|
"\n",
|
|
"id = starting_id\n",
|
|
"for p in problems:\n",
|
|
" pid = str(id).zfill(6)\n",
|
|
" NewProblem = CreateNewProblem(id = pid, content = p, origin = origin, dict = pro_dict)\n",
|
|
" pro_dict[pid] = NewProblem\n",
|
|
" id += 1\n",
|
|
"\n",
|
|
"#按id排序生成字典\n",
|
|
"sorted_dict_id = sorted(pro_dict)\n",
|
|
"sorted_dict = {}\n",
|
|
"for id in sorted_dict_id:\n",
|
|
" sorted_dict[id] = pro_dict[id]\n",
|
|
"#将排序后的字典转为json\n",
|
|
"new_database = json.dumps(sorted_dict,indent = 4,ensure_ascii=False)\n",
|
|
"#写入json数据库文件\n",
|
|
"with open(r\"../题库0.3/Problems.json\",\"w\",encoding = \"utf8\") as f:\n",
|
|
" f.write(new_database)"
|
|
]
|
|
}
|
|
],
|
|
"metadata": {
|
|
"kernelspec": {
|
|
"display_name": "Python 3.8.8 ('base')",
|
|
"language": "python",
|
|
"name": "python3"
|
|
},
|
|
"language_info": {
|
|
"codemirror_mode": {
|
|
"name": "ipython",
|
|
"version": 3
|
|
},
|
|
"file_extension": ".py",
|
|
"mimetype": "text/x-python",
|
|
"name": "python",
|
|
"nbconvert_exporter": "python",
|
|
"pygments_lexer": "ipython3",
|
|
"version": "3.8.8"
|
|
},
|
|
"orig_nbformat": 4,
|
|
"vscode": {
|
|
"interpreter": {
|
|
"hash": "d311ffef239beb3b8f3764271728f3972d7b090c974f8e972fcdeedf230299ac"
|
|
}
|
|
}
|
|
},
|
|
"nbformat": 4,
|
|
"nbformat_minor": 2
|
|
}
|