{ "cells": [ { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "题号: 014105 , 字段: tags 中已添加数据: 第一单元\n", "题号: 014107 , 字段: tags 中已添加数据: 第一单元\n", "题号: 014109 , 字段: tags 中已添加数据: 第一单元\n", "题号: 014111 , 字段: tags 中已添加数据: 第一单元\n", "题号: 014112 , 字段: tags 中已添加数据: 第一单元\n", "题号: 014117 , 字段: tags 中已添加数据: 第一单元\n", "题号: 014118 , 字段: tags 中已添加数据: 第一单元\n", "题号: 014120 , 字段: tags 中已添加数据: 第二单元\n", "题号: 014122 , 字段: tags 中已添加数据: 第一单元\n", "题号: 014133 , 字段: tags 中已添加数据: 第一单元\n", "题号: 014134 , 字段: tags 中已添加数据: 第一单元\n", "题号: 014136 , 字段: tags 中已添加数据: 第一单元\n", "题号: 014153 , 字段: tags 中已添加数据: 第一单元\n", "题号: 014157 , 字段: tags 中已添加数据: 第一单元\n", "题号: 014164 , 字段: tags 中已添加数据: 第一单元\n", "题号: 014165 , 字段: tags 中已添加数据: 第一单元\n", "题号: 014166 , 字段: tags 中已添加数据: 第二单元\n", "题号: 014167 , 字段: tags 中已添加数据: 第二单元\n", "题号: 014168 , 字段: tags 中已添加数据: 第二单元\n", "题号: 014169 , 字段: tags 中已添加数据: 第二单元\n", "题号: 014170 , 字段: tags 中已添加数据: 第二单元\n", "题号: 014171 , 字段: tags 中已添加数据: 第二单元\n", "题号: 014172 , 字段: tags 中已添加数据: 第二单元\n", "题号: 014174 , 字段: tags 中已添加数据: 第二单元\n", "题号: 014175 , 字段: tags 中已添加数据: 第二单元\n", "题号: 014176 , 字段: tags 中已添加数据: 第二单元\n", "题号: 014177 , 字段: tags 中已添加数据: 第二单元\n", "题号: 014178 , 字段: tags 中已添加数据: 第二单元\n", "题号: 014179 , 字段: tags 中已添加数据: 第二单元\n", "题号: 014180 , 字段: tags 中已添加数据: 第二单元\n", "题号: 014181 , 字段: tags 中已添加数据: 第二单元\n", "题号: 014182 , 字段: tags 中已添加数据: 第二单元\n", "题号: 014183 , 字段: tags 中已添加数据: 第二单元\n", "题号: 014190 , 字段: tags 中已添加数据: 第二单元\n", "题号: 014191 , 字段: tags 中已添加数据: 第二单元\n", "题号: 014192 , 字段: tags 中已添加数据: 第二单元\n", "题号: 014193 , 字段: tags 中已添加数据: 第二单元\n", "题号: 014194 , 字段: tags 中已添加数据: 第二单元\n", "题号: 014199 , 字段: tags 中已添加数据: 第二单元\n", "题号: 014203 , 字段: tags 中已添加数据: 第二单元\n", "题号: 014204 , 字段: tags 中已添加数据: 第三单元\n", "题号: 014205 , 字段: tags 中已添加数据: 第三单元\n", "题号: 014206 , 字段: tags 中已添加数据: 第三单元\n", "题号: 014207 , 字段: tags 中已添加数据: 第三单元\n", "题号: 014208 , 字段: tags 中已添加数据: 第三单元\n", "题号: 014217 , 字段: tags 中已添加数据: 第三单元\n", "题号: 014221 , 字段: tags 中已添加数据: 第三单元\n", "题号: 014226 , 字段: tags 中已添加数据: 第三单元\n", "题号: 014227 , 字段: tags 中已添加数据: 第三单元\n", "题号: 014231 , 字段: tags 中已添加数据: 第三单元\n", "题号: 014245 , 字段: tags 中已添加数据: 第三单元\n", "题号: 014246 , 字段: tags 中已添加数据: 第三单元\n", "题号: 014249 , 字段: tags 中已添加数据: 第三单元\n", "题号: 014255 , 字段: tags 中已添加数据: 第三单元\n", "题号: 014259 , 字段: tags 中已添加数据: 第三单元\n", "题号: 014260 , 字段: tags 中已添加数据: 第三单元\n", "题号: 014262 , 字段: tags 中已添加数据: 第二单元\n", "题号: 014262 , 字段: tags 中已添加数据: 第三单元\n", "题号: 014263 , 字段: tags 中已添加数据: 第三单元\n", "题号: 014265 , 字段: tags 中已添加数据: 第三单元\n", "题号: 014266 , 字段: tags 中已添加数据: 第三单元\n", "题号: 014269 , 字段: tags 中已添加数据: 第三单元\n", "题号: 014270 , 字段: tags 中已添加数据: 第三单元\n", "题号: 014273 , 字段: tags 中已添加数据: 第三单元\n", "题号: 014283 , 字段: tags 中已添加数据: 第三单元\n", "题号: 014285 , 字段: tags 中已添加数据: 第三单元\n", "题号: 014286 , 字段: tags 中已添加数据: 第三单元\n", "题号: 014288 , 字段: tags 中已添加数据: 第二单元\n", "题号: 014290 , 字段: tags 中已添加数据: 第二单元\n", "题号: 014330 , 字段: tags 中已添加数据: 第五单元\n", "题号: 014331 , 字段: tags 中已添加数据: 第五单元\n", "题号: 014339 , 字段: tags 中已添加数据: 第五单元\n", "题号: 014346 , 字段: tags 中已添加数据: 第六单元\n", "题号: 014348 , 字段: tags 中已添加数据: 第六单元\n", "题号: 014349 , 字段: tags 中已添加数据: 第六单元\n", "题号: 014352 , 字段: tags 中已添加数据: 第六单元\n", "题号: 014354 , 字段: tags 中已添加数据: 第六单元\n", "题号: 014359 , 字段: tags 中已添加数据: 第六单元\n", "题号: 014360 , 字段: tags 中已添加数据: 第六单元\n", "题号: 014364 , 字段: tags 中已添加数据: 第七单元\n", "题号: 014366 , 字段: tags 中已添加数据: 第七单元\n", "题号: 014367 , 字段: tags 中已添加数据: 第七单元\n", "题号: 014369 , 字段: tags 中已添加数据: 第七单元\n", "题号: 014373 , 字段: tags 中已添加数据: 第七单元\n", "题号: 014377 , 字段: tags 中已添加数据: 第五单元\n", "题号: 014380 , 字段: tags 中已添加数据: 第五单元\n", "题号: 014381 , 字段: tags 中已添加数据: 第五单元\n", "题号: 014382 , 字段: tags 中已添加数据: 第五单元\n", "题号: 014383 , 字段: tags 中已添加数据: 第五单元\n", "题号: 014386 , 字段: tags 中已添加数据: 第五单元\n", "题号: 014388 , 字段: tags 中已添加数据: 第五单元\n", "题号: 014392 , 字段: tags 中已添加数据: 第五单元\n", "题号: 014393 , 字段: tags 中已添加数据: 第五单元\n", "题号: 014394 , 字段: tags 中已添加数据: 第五单元\n", "题号: 014410 , 字段: tags 中已添加数据: 第六单元\n", "题号: 014412 , 字段: tags 中已添加数据: 第六单元\n", "题号: 014415 , 字段: tags 中已添加数据: 第六单元\n", "题号: 014419 , 字段: tags 中已添加数据: 第六单元\n", "题号: 014423 , 字段: tags 中已添加数据: 第六单元\n", "题号: 014426 , 字段: tags 中已添加数据: 第六单元\n", "题号: 014427 , 字段: tags 中已添加数据: 第六单元\n", "题号: 014432 , 字段: tags 中已添加数据: 第六单元\n", "题号: 014435 , 字段: tags 中已添加数据: 第六单元\n", "题号: 014436 , 字段: tags 中已添加数据: 第六单元\n", "题号: 014437 , 字段: tags 中已添加数据: 第六单元\n", "题号: 014438 , 字段: tags 中已添加数据: 第六单元\n", "题号: 014439 , 字段: tags 中已添加数据: 第六单元\n", "题号: 014440 , 字段: tags 中已添加数据: 第六单元\n", "题号: 014441 , 字段: tags 中已添加数据: 第六单元\n", "题号: 014443 , 字段: tags 中已添加数据: 第六单元\n", "题号: 014448 , 字段: tags 中已添加数据: 第六单元\n", "题号: 014451 , 字段: tags 中已添加数据: 第六单元\n", "题号: 014452 , 字段: tags 中已添加数据: 第六单元\n", "题号: 014454 , 字段: tags 中已添加数据: 第六单元\n", "题号: 014455 , 字段: tags 中已添加数据: 第六单元\n", "题号: 014464 , 字段: tags 中已添加数据: 第七单元\n", "题号: 014478 , 字段: tags 中已添加数据: 第七单元\n", "题号: 014483 , 字段: tags 中已添加数据: 第七单元\n", "题号: 014503 , 字段: tags 中已添加数据: 第七单元\n", "题号: 014505 , 字段: tags 中已添加数据: 第七单元\n", "题号: 014532 , 字段: tags 中已添加数据: 第四单元\n", "题号: 014533 , 字段: tags 中已添加数据: 第四单元\n", "题号: 014534 , 字段: tags 中已添加数据: 第四单元\n", "题号: 014538 , 字段: tags 中已添加数据: 第四单元\n", "题号: 014540 , 字段: tags 中已添加数据: 第四单元\n", "题号: 014546 , 字段: tags 中已添加数据: 第四单元\n", "题号: 031223 , 字段: tags 中已添加数据: 第一单元\n", "题号: 031227 , 字段: tags 中已添加数据: 第七单元\n", "题号: 031236 , 字段: tags 中已添加数据: 第二单元\n", "题号: 031266 , 字段: tags 中已添加数据: 第九单元\n", "题号: 031282 , 字段: tags 中已添加数据: 第七单元\n", "题号: 031311 , 字段: tags 中已添加数据: 第一单元\n", "题号: 031312 , 字段: tags 中已添加数据: 第二单元\n", "题号: 031313 , 字段: tags 中已添加数据: 第五单元\n", "题号: 031314 , 字段: tags 中已添加数据: 第三单元\n", "题号: 031315 , 字段: tags 中已添加数据: 第七单元\n", "题号: 031316 , 字段: tags 中已添加数据: 第二单元\n", "题号: 031317 , 字段: tags 中已添加数据: 第七单元\n", "题号: 031317 , 字段: tags 中已添加数据: 第五单元\n", "题号: 031318 , 字段: tags 中已添加数据: 第六单元\n", "题号: 031319 , 字段: tags 中已添加数据: 第三单元\n", "题号: 031320 , 字段: tags 中已添加数据: 第八单元\n", "题号: 031321 , 字段: tags 中已添加数据: 第一单元\n", "题号: 031321 , 字段: tags 中已添加数据: 第二单元\n", "题号: 031322 , 字段: tags 中已添加数据: 第七单元\n", "题号: 031323 , 字段: tags 中已添加数据: 第七单元\n", "题号: 031324 , 字段: tags 中已添加数据: 第二单元\n", "题号: 031325 , 字段: tags 中已添加数据: 第六单元\n", "题号: 031326 , 字段: tags 中已添加数据: 第四单元\n", "题号: 031327 , 字段: tags 中已添加数据: 第七单元\n", "题号: 031328 , 字段: tags 中已添加数据: 第九单元\n", "题号: 031329 , 字段: tags 中已添加数据: 第三单元\n", "题号: 031330 , 字段: tags 中已添加数据: 第七单元\n", "题号: 031331 , 字段: tags 中已添加数据: 第二单元\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().strip()\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": "pythontest", "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": "91219a98e0e9be72efb992f647fe78b593124968b75db0b865552d6787c8db93" } } }, "nbformat": 4, "nbformat_minor": 2 }