This repository has been archived on 2024-06-23. You can view files and clone it, but cannot push or open issues or pull requests.
mathdeptv2/工具/批量添加题库字段数据.ipynb

273 lines
15 KiB
Plaintext

{
"cells": [
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"题号: 030170 , 字段: objs 中已添加数据: K0802003B\n",
"题号: 030170 , 字段: objs 中已添加数据: K0803002B\n",
"题号: 030171 , 字段: objs 中已添加数据: K0803002B\n",
"题号: 030173 , 字段: objs 中已添加数据: K0803002B\n",
"题号: 030174 , 字段: objs 中已添加数据: K0803002B\n",
"题号: 030176 , 字段: objs 中已添加数据: K0803002B\n",
"题号: 030178 , 字段: objs 中已添加数据: K0803002B\n",
"题号: 030181 , 字段: objs 中已添加数据: K0803002B\n",
"题号: 030183 , 字段: objs 中已添加数据: K0803002B\n",
"题号: 030172 , 字段: objs 中已添加数据: K0805002B\n",
"题号: 030172 , 字段: objs 中已添加数据: K0806004B\n",
"题号: 030175 , 字段: objs 中已添加数据: K0806004B\n",
"题号: 030184 , 字段: objs 中已添加数据: K0806004B\n",
"题号: 030186 , 字段: objs 中已添加数据: K0806004B\n",
"题号: 030190 , 字段: objs 中已添加数据: K0806004B\n",
"题号: 030194 , 字段: objs 中已添加数据: K0806004B\n",
"题号: 030185 , 字段: objs 中已添加数据: K0807001B\n",
"题号: 030193 , 字段: objs 中已添加数据: K0807001B\n",
"题号: 030184 , 字段: objs 中已添加数据: K0808002B\n",
"题号: 030186 , 字段: objs 中已添加数据: K0808002B\n",
"题号: 030187 , 字段: objs 中已添加数据: K0808002B\n",
"题号: 030188 , 字段: objs 中已添加数据: K0808002B\n",
"题号: 030190 , 字段: objs 中已添加数据: K0808002B\n",
"题号: 030194 , 字段: objs 中已添加数据: K0808002B\n",
"题号: 030189 , 字段: objs 中已添加数据: K0809001B\n",
"题号: 030195 , 字段: objs 中已添加数据: K0901001B\n",
"题号: 030197 , 字段: objs 中已添加数据: K0901001B\n",
"题号: 030198 , 字段: objs 中已添加数据: K0901001B\n",
"题号: 030206 , 字段: objs 中已添加数据: K0901001B\n",
"题号: 030217 , 字段: objs 中已添加数据: K0901001B\n",
"题号: 030262 , 字段: objs 中已添加数据: K0901001B\n",
"题号: 030195 , 字段: objs 中已添加数据: K0901002B\n",
"题号: 030222 , 字段: objs 中已添加数据: K0901004B\n",
"题号: 030226 , 字段: objs 中已添加数据: K0902002B\n",
"题号: 030256 , 字段: objs 中已添加数据: K0902002B\n",
"题号: 030199 , 字段: objs 中已添加数据: K0902003B\n",
"题号: 030217 , 字段: objs 中已添加数据: K0902003B\n",
"题号: 030223 , 字段: objs 中已添加数据: K0902003B\n",
"题号: 030224 , 字段: objs 中已添加数据: K0902003B\n",
"题号: 030229 , 字段: objs 中已添加数据: K0902003B\n",
"题号: 030266 , 字段: objs 中已添加数据: K0902004B\n",
"题号: 030203 , 字段: objs 中已添加数据: K0902004B\n",
"题号: 030218 , 字段: objs 中已添加数据: K0902004B\n",
"题号: 030228 , 字段: objs 中已添加数据: K0902004B\n",
"题号: 030230 , 字段: objs 中已添加数据: K0902004B\n",
"题号: 030231 , 字段: objs 中已添加数据: K0902004B\n",
"题号: 030196 , 字段: objs 中已添加数据: K0903001B\n",
"题号: 030219 , 字段: objs 中已添加数据: K0903001B\n",
"题号: 030221 , 字段: objs 中已添加数据: K0903001B\n",
"题号: 030230 , 字段: objs 中已添加数据: K0903001B\n",
"题号: 030231 , 字段: objs 中已添加数据: K0903001B\n",
"题号: 030220 , 字段: objs 中已添加数据: K0903002B\n",
"题号: 030170 , 字段: objs 中已添加数据: K0903005B\n",
"题号: 030196 , 字段: objs 中已添加数据: K0903005B\n",
"题号: 030200 , 字段: objs 中已添加数据: K0903005B\n",
"题号: 030201 , 字段: objs 中已添加数据: K0903005B\n",
"题号: 030202 , 字段: objs 中已添加数据: K0903005B\n",
"题号: 030204 , 字段: objs 中已添加数据: K0903005B\n",
"题号: 030233 , 字段: objs 中已添加数据: K0903005B\n",
"题号: 030265 , 字段: objs 中已添加数据: K0903005B\n",
"题号: 030196 , 字段: objs 中已添加数据: K0903006B\n",
"题号: 030242 , 字段: objs 中已有该数据: K0904001B\n",
"题号: 030251 , 字段: objs 中已有该数据: K0904001B\n",
"题号: 030270 , 字段: objs 中已有该数据: K0904001B\n",
"题号: 030242 , 字段: objs 中已有该数据: K0904002B\n",
"题号: 030251 , 字段: objs 中已有该数据: K0904002B\n",
"题号: 030242 , 字段: objs 中已有该数据: K0904003B\n",
"题号: 030251 , 字段: objs 中已有该数据: K0904003B\n",
"题号: 030207 , 字段: objs 中已有该数据: K0904005B\n",
"题号: 030210 , 字段: objs 中已有该数据: K0904005B\n",
"题号: 030213 , 字段: objs 中已有该数据: K0904005B\n",
"题号: 030238 , 字段: objs 中已有该数据: K0904005B\n",
"题号: 030239 , 字段: objs 中已有该数据: K0904005B\n",
"题号: 030208 , 字段: objs 中已有该数据: K0905001B\n",
"题号: 030211 , 字段: objs 中已有该数据: K0905001B\n",
"题号: 030208 , 字段: objs 中已有该数据: K0905002B\n",
"题号: 030212 , 字段: objs 中已有该数据: K0905003B\n",
"题号: 030241 , 字段: objs 中已有该数据: K0905005B\n",
"题号: 030258 , 字段: objs 中已有该数据: K0905005B\n",
"题号: 030269 , 字段: objs 中已有该数据: K0905005B\n",
"题号: 030259 , 字段: objs 中已有该数据: K0906004B\n",
"题号: 030257 , 字段: objs 中已有该数据: K0906004B\n",
"题号: 030254 , 字段: objs 中已有该数据: K0906004B\n",
"题号: 030253 , 字段: objs 中已有该数据: K0906004B\n",
"题号: 030252 , 字段: objs 中已有该数据: K0906004B\n",
"题号: 030211 , 字段: objs 中已有该数据: K0906004B\n",
"题号: 030214 , 字段: objs 中已有该数据: K0906004B\n",
"题号: 030222 , 字段: objs 中已有该数据: K0906004B\n",
"题号: 030234 , 字段: objs 中已有该数据: K0906004B\n",
"题号: 030241 , 字段: objs 中已有该数据: K0906004B\n",
"题号: 030244 , 字段: objs 中已有该数据: K0906004B\n",
"题号: 030245 , 字段: objs 中已有该数据: K0906004B\n",
"题号: 030249 , 字段: objs 中已有该数据: K0906004B\n",
"题号: 030264 , 字段: objs 中已有该数据: K0906004B\n",
"题号: 030263 , 字段: objs 中已有该数据: K0906004B\n",
"题号: 030255 , 字段: objs 中已有该数据: K0906005B\n",
"题号: 030253 , 字段: objs 中已有该数据: K0906005B\n",
"题号: 030254 , 字段: objs 中已有该数据: K0906005B\n",
"题号: 030246 , 字段: objs 中已有该数据: K0906005B\n",
"题号: 030247 , 字段: objs 中已有该数据: K0906005B\n",
"题号: 030248 , 字段: objs 中已有该数据: K0906005B\n",
"题号: 030249 , 字段: objs 中已有该数据: K0906005B\n",
"题号: 030252 , 字段: objs 中已有该数据: K0906005B\n",
"题号: 030257 , 字段: objs 中已有该数据: K0906005B\n",
"题号: 030268 , 字段: objs 中已有该数据: K0906005B\n",
"题号: 030241 , 字段: objs 中已有该数据: K0906008B\n",
"题号: 030274 , 字段: objs 中已有该数据: K0906009B\n",
"题号: 030209 , 字段: objs 中已有该数据: K0906009B\n",
"题号: 030249 , 字段: objs 中已有该数据: K0906009B\n",
"题号: 030248 , 字段: objs 中已有该数据: K0906009B\n",
"题号: 030258 , 字段: objs 中已有该数据: K0906009B\n",
"题号: 030260 , 字段: objs 中已有该数据: K0906009B\n",
"题号: 030269 , 字段: objs 中已有该数据: K0906009B\n",
"题号: 030270 , 字段: objs 中已有该数据: K0906009B\n",
"题号: 030225 , 字段: objs 中已有该数据: K0906010B\n",
"题号: 030227 , 字段: objs 中已有该数据: K0906010B\n",
"题号: 030235 , 字段: objs 中已有该数据: K0906010B\n",
"题号: 030250 , 字段: objs 中已有该数据: K0906010B\n",
"题号: 030261 , 字段: objs 中已有该数据: K0906010B\n",
"题号: 030215 , 字段: objs 中已有该数据: K0907001B\n",
"题号: 030243 , 字段: objs 中已有该数据: K0907001B\n",
"题号: 030251 , 字段: objs 中已有该数据: K0907001B\n",
"题号: 030270 , 字段: objs 中已有该数据: K0907001B\n",
"题号: 030271 , 字段: objs 中已有该数据: K0907001B\n",
"题号: 030272 , 字段: objs 中已有该数据: K0907001B\n",
"题号: 030243 , 字段: objs 中已有该数据: K0907003B\n",
"题号: 030251 , 字段: objs 中已有该数据: K0907003B\n",
"题号: 030270 , 字段: objs 中已有该数据: K0907003B\n",
"题号: 030271 , 字段: objs 中已有该数据: K0907003B\n",
"题号: 030272 , 字段: objs 中已有该数据: K0907003B\n",
"题号: 030206 , 字段: objs 中已有该数据: K0907004B\n",
"题号: 030236 , 字段: objs 中已有该数据: K0907004B\n",
"题号: 030237 , 字段: objs 中已有该数据: K0907004B\n",
"题号: 030240 , 字段: objs 中已有该数据: K0907004B\n",
"题号: 030245 , 字段: objs 中已有该数据: K0907004B\n",
"题号: 030256 , 字段: objs 中已有该数据: K0907004B\n",
"题号: 030257 , 字段: objs 中已有该数据: K0907004B\n",
"题号: 030267 , 字段: objs 中已有该数据: K0907004B\n"
]
}
],
"source": [
"import os,re,json\n",
"\n",
"\"\"\"---明确数据文件位置---\"\"\"\n",
"datafile = \"文本文件/metadata.txt\"\n",
"# 双回车分隔,记录内单回车分隔列表,首行为字段名\n",
"\"\"\"---文件位置结束---\"\"\"\n",
"\n",
"def trim(string):\n",
" string = re.sub(r\"^[ \\t\\n]*\",\"\",string)\n",
" string = re.sub(r\"[ \\t\\n]*$\",\"\",string)\n",
" return string\n",
"def FloatToInt(string):\n",
" f = float(string)\n",
" if abs(f-round(f))<0.01:\n",
" f = round(f)\n",
" return f\n",
"\n",
"with open(datafile,\"r\",encoding=\"utf8\") as f:\n",
" data = f.read()\n",
"pos = data.index(\"\\n\")\n",
"field = data[:pos].strip()\n",
"appending_data = data[pos:]\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",
"with open(r\"../题库0.3/LessonObj.json\",\"r\",encoding = \"utf8\") as f:\n",
" database = f.read()\n",
"obj_dict = json.loads(database)\n",
"\n",
"#该字段列表可能需要更新\n",
"fields = [\"content\",\"objs\",\"tags\",\"genre\",\"ans\",\"solution\",\"duration\",\"usages\",\"origin\",\"edit\",\"same\",\"related\",\"remark\",\"space\"]\n",
"\n",
"if field in fields:\n",
" field_type = type(pro_dict[\"000001\"][field])\n",
" datalist = [record.strip() for record in appending_data.split(\"\\n\\n\") if len(trim(record)) > 0]\n",
" for record in datalist:\n",
" id = re.findall(r\"^[\\d]{1,}\",record)[0]\n",
" data = record[len(id):].strip()\n",
" id = id.zfill(6)\n",
" if not id in pro_dict:\n",
" print(\"题号:\",id,\"不在数据库中.\")\n",
" break\n",
" \n",
" #字符串类型字段添加数据\n",
" elif field_type == str and data in pro_dict[id][field]:\n",
" print(\"题号:\",id,\", 字段:\",field,\"中已有该数据:\",data)\n",
" elif field_type == str and not data in pro_dict[id][field] and not field == \"ans\" and not field == \"space\":\n",
" origin_data = pro_dict[id][field]\n",
" new_data = trim(origin_data + \"\\n\" + data)\n",
" pro_dict[id][field] = new_data\n",
" print(\"题号:\",id,\", 字段:\",field,\"中已添加数据:\",data)\n",
" elif field_type == str and not data in pro_dict[id][field] and field == \"ans\" or field == \"space\":\n",
" pro_dict[id][field] = data\n",
" print(\"题号:\",id,\", 字段:\",field,\"中已修改数据:\",data)\n",
" \n",
" #数值类型字段添加数据\n",
" elif (field_type == int or field_type == float) and abs(float(data) - pro_dict[id][field])<0.01:\n",
" print(\"题号:\",id,\", 字段:\",field,\"中已有该数据:\",FloatToInt(data))\n",
" elif (field_type == int or field_type == float) and abs(float(data) - pro_dict[id][field])>=0.01:\n",
" pro_dict[id][field] = FloatToInt(data)\n",
" print(\"题号:\",id,\", 字段:\",field,\"中已修改数据:\",FloatToInt(data))\n",
" \n",
" #列表类型字段添加数据\n",
" elif field_type == list:\n",
" cell_data_list = [d.strip() for d in data.split(\"\\n\")]\n",
" for cell_data in cell_data_list:\n",
" if cell_data in pro_dict[id][field]:\n",
" print(\"题号:\",id,\", 字段:\",field,\"中已有该数据:\",cell_data)\n",
" elif not field == \"objs\":\n",
" pro_dict[id][field].append(cell_data)\n",
" print(\"题号:\",id,\", 字段:\",field,\"中已添加数据:\",cell_data)\n",
" else:\n",
" if not cell_data in obj_dict and not cell_data.upper() == \"KNONE\":\n",
" print(\"题号:\",id,\", 字段:\",field,\"目标编号有误:\",cell_data)\n",
" else:\n",
" pro_dict[id][field].append(cell_data.upper())\n",
" print(\"题号:\",id,\", 字段:\",field,\"中已添加数据:\",cell_data.upper())\n",
"\n",
"with open(r\"../题库0.3/Problems.json\",\"w\",encoding = \"utf8\") as f:\n",
" f.write(json.dumps(pro_dict,indent=4,ensure_ascii=False))"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"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
}