From 84477bd0debfce35812671fcb381e2e076539c5a Mon Sep 17 00:00:00 2001 From: "weiye.wang" Date: Thu, 25 May 2023 21:09:38 +0800 Subject: [PATCH] =?UTF-8?q?=E6=9B=B4=E6=96=B0=E6=89=B9=E9=87=8F=E6=B7=BB?= =?UTF-8?q?=E5=8A=A0=E5=AD=97=E6=AE=B5=E6=95=B0=E6=8D=AE=E7=A8=8B=E5=BA=8F?= =?UTF-8?q?,=20=E9=81=87=E5=88=B0=E9=87=8D=E5=A4=8D=E7=9A=84=E6=95=B0?= =?UTF-8?q?=E6=8D=AE=E8=83=BD=E8=BF=9B=E8=A1=8C=E5=88=9D=E6=AD=A5=E7=9A=84?= =?UTF-8?q?=E5=88=A4=E6=96=AD,=20=E6=97=A0=E6=B3=95=E5=88=A4=E6=96=AD?= =?UTF-8?q?=E7=9A=84=E4=BC=9A=E8=BE=93=E5=87=BA=E4=BB=A5=E5=A4=87=E4=BA=8C?= =?UTF-8?q?=E6=AC=A1=E5=A4=84=E7=90=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- 工具/批量添加字段数据.py | 34 +++++++++++++++++++++++++++++++++- 1 file changed, 33 insertions(+), 1 deletion(-) diff --git a/工具/批量添加字段数据.py b/工具/批量添加字段数据.py index 3d29e931..38aa56cd 100644 --- a/工具/批量添加字段数据.py +++ b/工具/批量添加字段数据.py @@ -2,6 +2,7 @@ import os,re,json """---明确数据文件位置---""" datafile = "文本文件/metadata.txt" +pending_filename = "临时文件/pendingmetadata.txt" # 双回车分隔,记录内单回车分隔列表,首行为字段名 """---文件位置结束---""" @@ -34,6 +35,7 @@ fields = ["content","objs","tags","genre","ans","solution","duration","usages"," if field in fields: field_type = type(pro_dict["000001"][field]) #得到字段类型 datalist = [record.strip() for record in appending_data.split("\n\n") if len(trim(record)) > 0] #以连续两个回车为分隔切分要添加的数据 + pending_dict = {} for record in datalist: id = re.findall(r"^[\d]{1,}",record)[0] data = record[len(id):].strip() #每个记录最前方的连续数字作为题号(补全六位), 除题号外的部分作为内容 @@ -77,9 +79,39 @@ if field in fields: pro_dict[id][field].append(cell_data.upper()) print("题号:",id,", 字段:",field,"中已添加数据:",cell_data.upper()) elif field == "usages": #若字段为usages, 则先判断班级是否做过该题目, 随后做相应处理 - + if not cell_data[0] == "p": #若首字母不为p, 按常规方式处理 + cell_data = re.sub(r"\s+",r"\t",cell_data) + #print(id,cell_data) + classid = [info for info in cell_data.split("\t") if len(re.findall(r"[班高一二三]",info))>0][0] #读取班级号 + time_striped = re.sub(r"^\d{4,}\t","",cell_data) # 读取去除时间的数据 + # print(id,classid,time_striped) + if time_striped in "\n".join(pro_dict[id]["usages"]): # 去除时间的数据已有记录 + print("题号",id,classid,"已记录") + elif classid in "\n".join(pro_dict[id]["usages"]): # 班级已有记录但是去除时间的数据未有记录, 需人工处理 + print("题号",id,classid,"已使用. 在pending list中已记录.") + if not id in pending_dict: # 记录题号和班级均有重复的数据(可能是第二次做) + pending_dict[id] = [cell_data] + else: + pending_dict[id].append(cell_data) + else: + print("题号:",id,", 字段:",field,"中已添加数据:",cell_data.upper()) + pro_dict[id][field].append(cell_data.upper()) + else: #某行数据以p开始 + print("题号:",id,", 字段:",field,"中已强制添加数据:",cell_data.upper()[1:]) + pro_dict[id][field].append(cell_data.upper()[1:]) #强制添加该行数据(去除首字母p) + if len(pending_dict) > 0: + print("部分等待处理的数据已输出至",pending_filename) + output = "usages\n" + for id in pending_dict: + output += id + "\n" + for t in pending_dict[id]: + output += "p"+t+"\n" + output += "\n\n" + with open(pending_filename,"w",encoding="u8") as f: + f.write(output) + else: print("字段名有误")