import os,re,json

#范围定义在使用前需要替换
"""使用前替换范围定义"""
obj_range = "K0227001X:K0240999X"
"""范围定义到此结束"""


# 检查某一字符串是否在由,:的表达式给出的范围内
def within_range(string,list):
    flag = False
    for item in list:
        if string == item.strip():
            flag = True
            break
        elif ":" in item:
            start, end = item.split(":")
            if start <= string <= end:
                flag = True
                break
    return flag

# 读取课时目标数据库
with open(r"../题库0.3/LessonObj.json","r",encoding = "utf8") as f:
    database = f.read()
obj_dict = json.loads(database)

# 根据范围生成若干用于检查的闭区间范围
obj_range_list = obj_range.split(",")
output_string = ""

# 逐一选择目标, 并整合成表格的内容部分
for obj_id in obj_dict:
    if within_range(obj_id,obj_range_list):
        output_string += obj_id + " & " + obj_dict[obj_id]["content"] + " & " + r"\\ \hline" + "\n"

#输出到临时文件夹
with open(r"临时文件/课时目标提取结果.txt","w",encoding = "utf8") as f:
    f.write(output_string)

print("已输出至文件",r"临时文件/课时目标提取结果.txt")