{ "cells": [ { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "题号: 030757 , 字段: tags 中已添加数据: 第三单元\n", "题号: 030758 , 字段: tags 中已添加数据: 第三单元\n", "题号: 030759 , 字段: tags 中已添加数据: 第三单元\n", "题号: 030760 , 字段: tags 中已添加数据: 第三单元\n", "题号: 030761 , 字段: tags 中已添加数据: 第三单元\n", "题号: 030762 , 字段: tags 中已添加数据: 第三单元\n", "题号: 030763 , 字段: tags 中已添加数据: 第三单元\n", "题号: 030764 , 字段: tags 中已添加数据: 第三单元\n", "题号: 030765 , 字段: tags 中已添加数据: 第三单元\n", "题号: 030766 , 字段: tags 中已添加数据: 第三单元\n", "题号: 030767 , 字段: tags 中已添加数据: 第三单元\n", "题号: 030768 , 字段: tags 中已添加数据: 第三单元\n", "题号: 030769 , 字段: tags 中已添加数据: 第三单元\n", "题号: 030770 , 字段: tags 中已添加数据: 第三单元\n", "题号: 030771 , 字段: tags 中已添加数据: 第三单元\n", "题号: 030772 , 字段: tags 中已添加数据: 第三单元\n", "题号: 030773 , 字段: tags 中已添加数据: 第三单元\n", "题号: 030774 , 字段: tags 中已添加数据: 第三单元\n", "题号: 030775 , 字段: tags 中已添加数据: 第三单元\n", "题号: 030776 , 字段: tags 中已添加数据: 第三单元\n", "题号: 030777 , 字段: tags 中已添加数据: 第三单元\n", "题号: 030778 , 字段: tags 中已添加数据: 第三单元\n", "题号: 030779 , 字段: tags 中已添加数据: 第三单元\n", "题号: 030780 , 字段: tags 中已添加数据: 第三单元\n", "题号: 030781 , 字段: tags 中已添加数据: 第三单元\n", "题号: 030782 , 字段: tags 中已添加数据: 第三单元\n", "题号: 030783 , 字段: tags 中已添加数据: a\n", "题号: 030783 , 字段: tags 中已添加数据: 第三单元\n", "题号: 030784 , 字段: tags 中已添加数据: 第三单元\n", "题号: 030785 , 字段: tags 中已添加数据: 第三单元\n", "题号: 030786 , 字段: tags 中已添加数据: 第三单元\n", "题号: 030787 , 字段: tags 中已添加数据: 第三单元\n", "题号: 030788 , 字段: tags 中已添加数据: 第三单元\n", "题号: 030789 , 字段: tags 中已添加数据: 第三单元\n", "题号: 030790 , 字段: tags 中已添加数据: 第三单元\n", "题号: 030791 , 字段: tags 中已添加数据: 第三单元\n", "题号: 030792 , 字段: tags 中已添加数据: 第三单元\n", "题号: 030793 , 字段: tags 中已添加数据: 第三单元\n", "题号: 030794 , 字段: tags 中已添加数据: 第三单元\n", "题号: 030795 , 字段: tags 中已添加数据: 第三单元\n", "题号: 030796 , 字段: tags 中已添加数据: 第三单元\n", "题号: 030797 , 字段: tags 中已添加数据: 第三单元\n", "题号: 030798 , 字段: tags 中已添加数据: 第三单元\n", "题号: 030799 , 字段: tags 中已添加数据: 第三单元\n", "题号: 030800 , 字段: tags 中已添加数据: 第三单元\n", "题号: 030801 , 字段: tags 中已添加数据: 第三单元\n", "题号: 030802 , 字段: tags 中已添加数据: 第三单元\n", "题号: 030803 , 字段: tags 中已添加数据: 第三单元\n", "题号: 030804 , 字段: tags 中已添加数据: 第三单元\n", "题号: 030805 , 字段: tags 中已添加数据: 第三单元\n", "题号: 030806 , 字段: tags 中已添加数据: 第三单元\n", "题号: 030807 , 字段: tags 中已添加数据: 第三单元\n", "题号: 030808 , 字段: tags 中已添加数据: 第三单元\n", "题号: 030809 , 字段: tags 中已添加数据: 第三单元\n", "题号: 030810 , 字段: tags 中已添加数据: 第三单元\n", "题号: 030811 , 字段: tags 中已添加数据: 第三单元\n", "题号: 030812 , 字段: tags 中已添加数据: 第三单元\n", "题号: 030813 , 字段: tags 中已添加数据: 第三单元\n", "题号: 030814 , 字段: tags 中已添加数据: 第三单元\n", "题号: 030815 , 字段: tags 中已添加数据: 第三单元\n", "题号: 030816 , 字段: tags 中已添加数据: 第三单元\n", "题号: 030817 , 字段: tags 中已添加数据: 第三单元\n", "题号: 030818 , 字段: tags 中已添加数据: 第三单元\n", "题号: 030819 , 字段: tags 中已添加数据: 第三单元\n", "题号: 030820 , 字段: tags 中已添加数据: 第三单元\n", "题号: 030821 , 字段: tags 中已添加数据: 第三单元\n", "题号: 030822 , 字段: tags 中已添加数据: 第三单元\n", "题号: 030823 , 字段: tags 中已添加数据: 第三单元\n", "题号: 030824 , 字段: tags 中已添加数据: 第三单元\n", "题号: 030825 , 字段: tags 中已添加数据: 第三单元\n", "题号: 030826 , 字段: tags 中已添加数据: 第三单元\n", "题号: 030827 , 字段: tags 中已添加数据: 第三单元\n", "题号: 030828 , 字段: tags 中已添加数据: 第三单元\n", "题号: 030829 , 字段: tags 中已添加数据: 第三单元\n", "题号: 030830 , 字段: tags 中已添加数据: 第三单元\n", "题号: 030831 , 字段: tags 中已添加数据: 第三单元\n", "题号: 030832 , 字段: tags 中已添加数据: 第三单元\n", "题号: 030833 , 字段: tags 中已添加数据: 第三单元\n", "题号: 030834 , 字段: tags 中已添加数据: 第三单元\n", "题号: 030835 , 字段: tags 中已添加数据: 第三单元\n", "题号: 030836 , 字段: 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()\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": "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 }