{ "cells": [ { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "#修改起始id,出处,文件名\n", "starting_id = 30515\n", "origin = \"空中课堂选择性必修分布与成对数据补充习题\"\n", "filename = r\"C:\\Users\\weiye\\Documents\\wwy sync\\临时工作区\\自拟题目.tex\"\n", "editor = \"20230101\\t王伟叶\"" ] }, { "cell_type": "code", "execution_count": 20, "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,editor):\n", " NewProblem = CreateEmptyProblem(dict[\"000001\"])\n", " NewProblem[\"id\"] = str(id).zfill(6)\n", " NewProblem[\"content\"] = content\n", " NewProblem[\"origin\"] = origin\n", " NewProblem[\"edit\"] = [editor]\n", " return NewProblem\n", "\n", "duplicate_flag = False\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", " if pid in pro_dict:\n", " duplicate_flag = True\n", " NewProblem = CreateNewProblem(id = pid, content = p, origin = origin , dict = pro_dict,editor = editor)\n", " # NewProblem = CreateNewProblem(id = pid, content = p, origin = origin + \"试题\" + str(id- starting_id+1), dict = pro_dict,editor = editor)\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", "\n", "if not duplicate_flag:\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)\n", "else:\n", " print(\"题号有重复, 请检查.\\n\"*5)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "mathdept", "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.9.15" }, "orig_nbformat": 4, "vscode": { "interpreter": { "hash": "ff3c292c316ba85de6f1ad75f19c731e79d694e741b6f515ec18f14996fe48dc" } } }, "nbformat": 4, "nbformat_minor": 2 }