更新批量添加字段数据程序, 遇到重复的数据能进行初步的判断, 无法判断的会输出以备二次处理
This commit is contained in:
parent
71ce5c5e23
commit
84477bd0de
|
|
@ -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("字段名有误")
|
||||
|
||||
|
|
|
|||
Reference in New Issue