{ "cells": [ { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "题号: 009733 , 字段: objs 中已添加数据: K0801001B\n", "题号: 009734 , 字段: objs 中已添加数据: K0801001B\n", "题号: 009735 , 字段: objs 中已添加数据: K0802002B\n", "题号: 009736 , 字段: objs 中已添加数据: K0802003B\n", "题号: 009737 , 字段: objs 中已添加数据: K0802003B\n", "题号: 010539 , 字段: objs 中已添加数据: K0802003B\n", "题号: 010540 , 字段: objs 中已添加数据: K0802003B\n", "题号: 009736 , 字段: objs 中已添加数据: K0802004B\n", "题号: 009737 , 字段: objs 中已添加数据: K0802004B\n", "题号: 010539 , 字段: objs 中已添加数据: K0802004B\n", "题号: 010540 , 字段: objs 中已添加数据: K0802004B\n", "题号: 010534 , 字段: objs 中已添加数据: K0802005B\n", "题号: 010535 , 字段: objs 中已添加数据: K0802005B\n", "题号: 010537 , 字段: objs 中已添加数据: K0802005B\n", "题号: 010538 , 字段: objs 中已添加数据: K0802005B\n", "题号: 000223 , 字段: objs 中已添加数据: K0802006B\n", "题号: 009735 , 字段: objs 中已添加数据: K0802006B\n", "题号: 010536 , 字段: objs 中已添加数据: K0802006B\n", "题号: 009409 , 字段: objs 中已添加数据: K0803002B\n", "题号: 009410 , 字段: objs 中已添加数据: K0803002B\n", "题号: 009738 , 字段: objs 中已添加数据: K0803002B\n", "题号: 009739 , 字段: objs 中已添加数据: K0803002B\n", "题号: 009740 , 字段: objs 中已添加数据: K0803002B\n", "题号: 009741 , 字段: objs 中已添加数据: K0803002B\n", "题号: 010541 , 字段: objs 中已添加数据: K0803002B\n", "题号: 010542 , 字段: objs 中已添加数据: K0803002B\n", "题号: 010547 , 字段: objs 中已添加数据: K0803002B\n", "题号: 010548 , 字段: objs 中已添加数据: K0803002B\n", "题号: 010549 , 字段: objs 中已添加数据: K0803002B\n", "题号: 010554 , 字段: objs 中已添加数据: K0803002B\n", "题号: 010556 , 字段: objs 中已添加数据: K0803002B\n", "题号: 003887 , 字段: objs 中已添加数据: K0803002B\n", "题号: 003914 , 字段: objs 中已添加数据: K0803002B\n", "题号: 003993 , 字段: objs 中已添加数据: K0803002B\n", "题号: 004031 , 字段: objs 中已添加数据: K0803002B\n", "题号: 004110 , 字段: objs 中已添加数据: K0803002B\n", "题号: 009349 , 字段: objs 中已添加数据: K0803002B\n", "题号: 009354 , 字段: objs 中已添加数据: K0803002B\n", "题号: 009364 , 字段: objs 中已添加数据: K0803002B\n", "题号: 004193 , 字段: objs 中已添加数据: K0803002B\n", "题号: 004297 , 字段: objs 中已添加数据: K0803002B\n", "题号: 004341 , 字段: objs 中已添加数据: K0803002B\n", "题号: 004535 , 字段: objs 中已添加数据: K0803002B\n", "题号: 004612 , 字段: objs 中已添加数据: K0803002B\n", "题号: 004647 , 字段: objs 中已添加数据: K0803002B\n", "题号: 004731 , 字段: objs 中已添加数据: K0803002B\n", "题号: 009346 , 字段: objs 中已添加数据: K0803002B\n", "题号: 009347 , 字段: objs 中已添加数据: K0803002B\n", "题号: 002645 , 字段: objs 中已添加数据: K0803002B\n", "题号: 002650 , 字段: objs 中已添加数据: K0803002B\n", "题号: 002651 , 字段: objs 中已添加数据: K0803002B\n", "题号: 002662 , 字段: objs 中已添加数据: K0803002B\n", "题号: 002663 , 字段: objs 中已添加数据: K0803002B\n", "题号: 002664 , 字段: objs 中已添加数据: K0803002B\n", "题号: 003585 , 字段: objs 中已添加数据: K0803002B\n", "题号: 003598 , 字段: objs 中已添加数据: K0803002B\n", "题号: 003660 , 字段: objs 中已添加数据: K0803002B\n", "题号: 003727 , 字段: objs 中已添加数据: K0803002B\n", "题号: 003787 , 字段: objs 中已添加数据: K0803002B\n", "题号: 003872 , 字段: objs 中已添加数据: K0803002B\n", "题号: 000512 , 字段: objs 中已添加数据: K0803002B\n", "题号: 000564 , 字段: objs 中已添加数据: K0803002B\n", "题号: 000581 , 字段: objs 中已添加数据: K0803002B\n", "题号: 000601 , 字段: objs 中已添加数据: K0803002B\n", "题号: 000654 , 字段: objs 中已添加数据: K0803002B\n", "题号: 000659 , 字段: objs 中已添加数据: K0803002B\n", "题号: 000744 , 字段: objs 中已添加数据: K0803002B\n", "题号: 000773 , 字段: objs 中已添加数据: K0803002B\n", "题号: 000779 , 字段: objs 中已添加数据: K0803002B\n", "题号: 000829 , 字段: objs 中已添加数据: K0803002B\n", "题号: 000844 , 字段: objs 中已添加数据: K0803002B\n", "题号: 000969 , 字段: objs 中已添加数据: K0803002B\n", "题号: 002640 , 字段: objs 中已添加数据: K0803002B\n", "题号: 000218 , 字段: objs 中已有该数据: K0803002B\n", "题号: 000219 , 字段: objs 中已有该数据: K0803002B\n", "题号: 000220 , 字段: objs 中已有该数据: K0803002B\n", "题号: 000221 , 字段: objs 中已有该数据: K0803002B\n", "题号: 000222 , 字段: objs 中已有该数据: K0803002B\n", "题号: 000223 , 字段: objs 中已有该数据: K0803002B\n", "题号: 000227 , 字段: objs 中已有该数据: K0803002B\n", "题号: 000228 , 字段: objs 中已有该数据: K0803002B\n", "题号: 000332 , 字段: objs 中已添加数据: K0803002B\n", "题号: 000384 , 字段: objs 中已添加数据: K0803002B\n", "题号: 000391 , 字段: objs 中已添加数据: K0803002B\n", "题号: 000401 , 字段: objs 中已添加数据: K0803002B\n", "题号: 000420 , 字段: objs 中已添加数据: K0803002B\n", "题号: 000714 , 字段: objs 中已添加数据: K0805002B\n", "题号: 009743 , 字段: objs 中已添加数据: K0805002B\n", "题号: 010543 , 字段: objs 中已添加数据: K0805002B\n", "题号: 010546 , 字段: objs 中已添加数据: K0805002B\n", "题号: 010544 , 字段: objs 中已添加数据: K0805003B\n", "题号: 010550 , 字段: objs 中已添加数据: K0805003B\n", "题号: 000231 , 字段: objs 中已添加数据: K0805006B\n", "题号: 000624 , 字段: objs 中已添加数据: K0806002B\n", "题号: 009751 , 字段: objs 中已添加数据: K0806002B\n", "题号: 004324 , 字段: objs 中已添加数据: K0806004B\n", "题号: 004750 , 字段: objs 中已添加数据: K0806004B\n", "题号: 009366 , 字段: objs 中已添加数据: K0806004B\n", "题号: 009739 , 字段: objs 中已添加数据: K0806004B\n", "题号: 009741 , 字段: objs 中已添加数据: K0806004B\n", "题号: 009745 , 字段: objs 中已添加数据: K0806004B\n", "题号: 009746 , 字段: objs 中已添加数据: K0806004B\n", "题号: 010545 , 字段: objs 中已添加数据: K0806004B\n", "题号: 010546 , 字段: objs 中已添加数据: K0806004B\n", "题号: 010550 , 字段: objs 中已添加数据: K0806004B\n", "题号: 010551 , 字段: objs 中已添加数据: K0806004B\n", "题号: 010557 , 字段: objs 中已添加数据: K0806004B\n", "题号: 010558 , 字段: objs 中已添加数据: K0806004B\n", "题号: 010559 , 字段: objs 中已添加数据: K0806004B\n", "题号: 010560 , 字段: objs 中已添加数据: K0806004B\n", "题号: 000225 , 字段: objs 中已有该数据: K0806004B\n", "题号: 000227 , 字段: objs 中已添加数据: K0806004B\n", "题号: 000229 , 字段: objs 中已有该数据: K0806004B\n", "题号: 000231 , 字段: objs 中已添加数据: K0806004B\n", "题号: 000384 , 字段: objs 中已添加数据: K0806004B\n", "题号: 000512 , 字段: objs 中已添加数据: K0806004B\n", "题号: 000564 , 字段: objs 中已添加数据: K0806004B\n", "题号: 000672 , 字段: objs 中已添加数据: K0806004B\n", "题号: 000685 , 字段: objs 中已添加数据: K0806004B\n", "题号: 000695 , 字段: objs 中已添加数据: K0806004B\n", "题号: 000704 , 字段: objs 中已添加数据: K0806004B\n", "题号: 000714 , 字段: objs 中已添加数据: K0806004B\n", "题号: 000812 , 字段: objs 中已添加数据: K0806004B\n", "题号: 002645 , 字段: objs 中已添加数据: K0806004B\n", "题号: 003598 , 字段: objs 中已添加数据: K0806004B\n", "题号: 004039 , 字段: objs 中已添加数据: K0806004B\n", "题号: 004087 , 字段: objs 中已添加数据: K0806004B\n", "题号: 004150 , 字段: objs 中已添加数据: K0806004B\n", "题号: 009744 , 字段: objs 中已添加数据: K0806005B\n", "题号: 010553 , 字段: objs 中已添加数据: K0807001B\n", "题号: 009356 , 字段: objs 中已添加数据: K0807002B\n", "题号: 000224 , 字段: objs 中已有该数据: K0807003B\n", "题号: 000230 , 字段: objs 中已有该数据: K0807003B\n", "题号: 003734 , 字段: objs 中已添加数据: K0807003B\n", "题号: 009357 , 字段: objs 中已添加数据: K0807003B\n", "题号: 009747 , 字段: objs 中已添加数据: K0807003B\n", "题号: 009748 , 字段: objs 中已添加数据: K0807003B\n", "题号: 009749 , 字段: objs 中已添加数据: K0807003B\n", "题号: 010552 , 字段: objs 中已添加数据: K0807003B\n", "题号: 010553 , 字段: objs 中已添加数据: K0807003B\n", "题号: 010554 , 字段: objs 中已添加数据: K0807003B\n", "题号: 000226 , 字段: objs 中已添加数据: K0808002B\n", "题号: 000231 , 字段: objs 中已有该数据: K0808002B\n", "题号: 000611 , 字段: objs 中已添加数据: K0808002B\n", "题号: 000624 , 字段: objs 中已添加数据: K0808002B\n", "题号: 000685 , 字段: objs 中已添加数据: K0808002B\n", "题号: 000695 , 字段: objs 中已添加数据: K0808002B\n", "题号: 000704 , 字段: objs 中已添加数据: K0808002B\n", "题号: 000812 , 字段: objs 中已添加数据: K0808002B\n", "题号: 004039 , 字段: objs 中已添加数据: K0808002B\n", "题号: 004087 , 字段: objs 中已添加数据: K0808002B\n", "题号: 004150 , 字段: objs 中已添加数据: K0808002B\n", "题号: 004324 , 字段: objs 中已添加数据: K0808002B\n", "题号: 004750 , 字段: objs 中已添加数据: K0808002B\n", "题号: 009750 , 字段: objs 中已添加数据: K0808002B\n", "题号: 009751 , 字段: objs 中已添加数据: K0808002B\n", "题号: 009753 , 字段: objs 中已添加数据: K0808002B\n", "题号: 009754 , 字段: objs 中已添加数据: K0808002B\n", "题号: 010555 , 字段: objs 中已添加数据: K0808002B\n", "题号: 010558 , 字段: objs 中已添加数据: K0808002B\n", "题号: 010557 , 字段: objs 中已添加数据: K0808002B\n", "题号: 010559 , 字段: objs 中已添加数据: K0808002B\n", "题号: 010560 , 字段: objs 中已添加数据: K0808002B\n", "题号: 009751 , 字段: objs 中已添加数据: K0808003B\n", "题号: 009753 , 字段: objs 中已添加数据: K0808003B\n", "题号: 010557 , 字段: objs 中已添加数据: K0808003B\n", "题号: 010558 , 字段: objs 中已添加数据: K0808003B\n", "题号: 009754 , 字段: objs 中已添加数据: K0808004B\n", "题号: 009752 , 字段: objs 中已添加数据: K0809001B\n", "题号: 009753 , 字段: objs 中已添加数据: K0809002B\n", "题号: 009754 , 字段: objs 中已添加数据: K0809003B\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 }