import json from datetime import datetime, timedelta import math # 获取开始时间和结束时间 start_time = datetime.strptime("00:00:00", "%H:%M:%S") end_time = datetime.strptime("23:59:00", "%H:%M:%S") # 创建字典,以时间字符串为键,值为所属时间段的开始时间的字符串表示 data = {} x0a=datetime.strptime("09:00:00", "%H:%M:%S") x1=datetime.strptime("10:15:00", "%H:%M:%S") x1a=datetime.strptime("10:30:00", "%H:%M:%S") x3a=datetime.strptime("11:30:00", "%H:%M:%S") # 6 x3b=datetime.strptime("13:30:00", "%H:%M:%S") # 6 x7=datetime.strptime("15:00:00", "%H:%M:%S") x7a=datetime.strptime("21:00:00", "%H:%M:%S") x8=datetime.strptime("23:00:00", "%H:%M:%S") # 生成时间段数据 current_time = start_time m=dict(m5=0,m5a=[],m15=0,m15a=[],m30=0,m30a=[],m60=0,m60a=[]) while current_time <= end_time: key = current_time.strftime('%H:%M:%S') t2=int(key[:2]) t1=math.ceil( int(key[-5:-3])/5)*5 r={} print(key) if(current_time>x0a and current_time<=x7): if(current_time>x1 and current_time<=x1a): pass elif (current_time>x3a and current_time<=x3b): pass else: m["m5"]+=1 if(m["m5"]==5): m["m5"]=0 m["m5a"].append(dict(a=key,b=key)) else: m["m5a"].append(dict(a=key,b="")) m["m15"]+=1 if(m["m15"]==15): m["m15"]=0 m["m15a"].append(dict(a=key,b=key)) else: m["m15a"].append(dict(a=key,b="")) m["m30"]+=1 if(m["m30"]==30 or key=="15:00:00"): m["m30"]=0 m["m30a"].append(dict(a=key,b=key)) else: m["m30a"].append(dict(a=key,b="")) m["m60"]+=1 if(m["m60"]==60 or key=="15:00:00"): m["m60"]=0 m["m60a"].append(dict(a=key,b=key)) else: m["m60a"].append(dict(a=key,b="")) if(key=="15:10:00"): m["m5"]=0 m["m15"]=0 m["m30"]=0 m["m60"]=0 if(current_time>x7a and current_time<=x8): m["m5"]+=1 if(m["m5"]==5): m["m5"]=0 m["m5a"].append(dict(a=key,b=key)) else: m["m5a"].append(dict(a=key,b="")) m["m15"]+=1 if(m["m15"]==15): m["m15"]=0 m["m15a"].append(dict(a=key,b=key)) else: m["m15a"].append(dict(a=key,b="")) m["m30"]+=1 if(m["m30"]==30 or key=="23:00:00"): m["m30"]=0 m["m30a"].append(dict(a=key,b=key)) else: m["m30a"].append(dict(a=key,b="")) m["m60"]+=1 if(m["m60"]==60 or key=="23:00:00"): m["m60"]=0 m["m60a"].append(dict(a=key,b=key)) else: m["m60a"].append(dict(a=key,b="")) current_time += timedelta(minutes=1) xy="" for i in range(len(m["m5a"])): xx=m["m5a"][-i-1]["b"] if(xx!=""): xy=xx else: m["m5a"][-i-1]["b"]=xy xy="" for i in range(len(m["m15a"])): xx=m["m15a"][-i-1]["b"] if(xx!=""): xy=xx else: m["m15a"][-i-1]["b"]=xy xy="" for i in range(len(m["m30a"])): xx=m["m30a"][-i-1]["b"] if(xx!=""): xy=xx else: m["m30a"][-i-1]["b"]=xy xy="" for i in range(len(m["m60a"])): xx=m["m60a"][-i-1]["b"] if(xx!=""): xy=xx else: m["m60a"][-i-1]["b"]=xy r={} for i in range(len(m["m5a"])): x_m5=m["m5a"][i]["a"] m5=m["m5a"][i]["b"] for i in range(len(m["m15a"])): x_m15=m["m15a"][i]["a"] if(x_m5==x_m15): m15=m["m15a"][i]["b"] break for i in range(len(m["m30a"])): x_m15=m["m30a"][i]["a"] if(x_m5==x_m15): m30=m["m30a"][i]["b"] break for i in range(len(m["m60a"])): x_m15=m["m60a"][i]["a"] if(x_m5==x_m15): m60=m["m60a"][i]["b"] break data[x_m5]=dict(m5=m5,m15=m15,m30=m30,m60=m60) # 将字典保存为 JSON 文件 with open('data.json', 'w') as file: json.dump(data, file, indent=4)