import os,re,json outputfile = "临时文件/已做题目.txt" new_dir = r"C:\Users\weiye\Documents\wwy sync\23届\四月错题重做" old_dirs = [ r"C:\Users\weiye\Documents\wwy sync\23届\暑假概率初步续", r"C:\Users\weiye\Documents\wwy sync\23届\上学期测验卷", r"C:\Users\weiye\Documents\wwy sync\23届\上学期周末卷", r"C:\Users\weiye\Documents\wwy sync\23届\第一轮复习讲义", r"C:\Users\weiye\Documents\wwy sync\23届\其他讲义与试卷", r"C:\Users\weiye\Documents\wwy sync\23届\赋能", r"C:\Users\weiye\Documents\wwy sync\23届\正态分布及成对数据新课", r"C:\Users\weiye\Documents\wwy sync\23届\下学期测验卷", r"C:\Users\weiye\Documents\wwy sync\23届\下学期周末卷", r"C:\Users\weiye\Documents\wwy sync\23届\第二轮复习讲义" ] def id_related(id,prodict,idrespdict): for rid in prodict[id]["related"]: if rid in idrespdict: return True return False with open(r"../题库0.3/Problems.json","r",encoding="u8") as f: data = f.read() pro_dict = json.loads(data) id_resp_dict = {} for dir in old_dirs: for texfile in os.listdir(dir): if ".tex" in texfile: with open(os.path.join(dir,texfile),"r",encoding="u8") as f: data = f.read() ids = re.findall(r"\((\d{6})\)",data) for id in ids: if not id in id_resp_dict: id_resp_dict[id] = [os.path.join(os.path.split(dir)[1],texfile[:-4])+"--题序"+str(ids.index(id)+1)] else: id_resp_dict[id].append(os.path.join(os.path.split(dir)[1],texfile[:-4])+"--题序"+str(ids.index(id)+1)) new_texfiles = [f for f in os.listdir(new_dir) if ".tex" in f] output = "" for texfile in new_texfiles: with open(os.path.join(new_dir,texfile),"r",encoding="u8") as f: data = f.read() ids = re.findall(r"\((\d{6})\)",data) output += texfile[:-4] + "\n" print(texfile[:-4]) output += ",".join(ids) + "\n" print(",".join(ids)) for id in ids: if id in id_resp_dict: print("(%s)\t(已做题)\t%s"%(id,",".join(id_resp_dict[id]))) output += "(%s)\t(已做题)\t%s\n"%(id,",".join(id_resp_dict[id])) elif id_related: rel_ids = pro_dict[id]["related"] origins = [] for id in rel_ids: if id in id_resp_dict: origins += id_resp_dict[id] print("(%s)\t(关联题)\t%s"%(id,",".join(origins))) output += "(%s)\t(关联题)\t%s\n"%(id,",".join(origins)) else: print("(%s)\t(无关题)") output += "(%s)\t(无关题)\n" print("") output += "\n" with open(outputfile,"w",encoding="u8") as f: f.write(output)