简述:原代码一直报错,提示索引超出范围"IndexError: tuple index out of range"
一、原代码:
# -*- coding: utf-8 -*- import pymysql, datetime # 获取框架合约数据 def frame_treaty_get(frame_treaty_no_list): frame_treaty_list = [] for frame_treaty_no in frame_treaty_no_list: sql = f"select frame_treaty_no,start_date,due_date from frame_treaty where frame_treaty_no = '{frame_treaty_no}'" frame_treaty_list.append(conn_db(sql)) return frame_treaty_list # 连接数据库 def conn_db(sql): conn = pymysql.connect(host='10.xxx.66.84', port=3306, user='test', password='test@1234', db='test', charset="utf8") cursor = conn.cursor() cursor.execute(sql) results = cursor.fetchall() return results # 获取预估流量费 def est_flow_charge_get(dates): sql = f"select * from est_flow_charge where trading_day ='{dates}'" est_flow_charge_list = conn_db(sql) # print(len(est_flow_charge_list)) frame_treaty_no_list = [] for i in range(len(est_flow_charge_list)): frame_treaty_no_list.append(est_flow_charge_list[i][1]) # if est_flow_charge_list[0][1] # print(est_flow_charge_list[0][1]) # print(frame_treaty_no_list) frame_treaty_list = frame_treaty_get(frame_treaty_no_list) frame_treaty_eff_list = [] # print(frame_treaty_list) # print(len(frame_treaty_list)) if len(frame_treaty_list) == 0: print('合约列表为空') else: for index in range(len(frame_treaty_list)): if dates >= frame_treaty_list[index][1] and dates<= frame_treaty_list[index][2]: frame_treaty_eff_list.append(frame_treaty_list[index][0]) for index in range(len(frame_treaty_no_list)): if frame_treaty_no_list[index] in frame_treaty_eff_list: print(str(dates)+'日, '+str(frame_treaty_no_list[index])+'应产生预估流量费,一致') else: print(str(dates)+'日, '+str(frame_treaty_no_list[index])+'不应产生预估流量费,错误') for index in range(len(frame_treaty_eff_list)): if frame_treaty_eff_list[index] in frame_treaty_list: print(str(dates)+'日, '+str(frame_treaty_no_list[index])+'是存续期合约,已产生预估流量费,一致') else: print(str(dates)+'日, '+str(frame_treaty_no_list[index])+'是存续期合约,未产生预估流量费,错误') if __name__ == '__main__': dates = '2023-12-01' # dates = datetime.date.today() 获取当日日期 est_flow_charge_get(dates)
二、解决过程
1.解决方法:
debug + Evaluate Expression2.bug原因:
frame_treaty_list列表里不是直接就是元组(合约编号, 合约起始日, 合约到期日),而是元组里套元组((合约编号, 合约起始日, 合约到期日),),所以需要取两次
三、解决后的代码:
# -*- coding: utf-8 -*- import pymysql, datetime # 获取frame_treaty_no_list列表,所有合约的起始日、到期日 def frame_treaty_get(frame_treaty_no_list): frame_treaty_list = [] for frame_treaty_no in frame_treaty_no_list: sql = f"select frame_treaty_no,start_date,due_date from frame_treaty where frame_treaty_no = '{frame_treaty_no}'" frame_treaty_list.append(conn_db(sql)) return frame_treaty_list # 连接数据库 def conn_db(sql): conn = pymysql.connect(host='10.xxx.66.84', port=3306, user='test', password='test@1234', db='test', charset="utf8") cursor = conn.cursor() cursor.execute(sql) results = cursor.fetchall() return results # 获取预估流量费 def est_flow_charge_get(dates): sql = f"select * from est_flow_charge where trading_day ='{dates}'" est_flow_charge_list = conn_db(sql) # print(len(est_flow_charge_list)) frame_treaty_no_list = [] for i in range(len(est_flow_charge_list)): frame_treaty_no_list.append(est_flow_charge_list[i][1]) # if est_flow_charge_list[0][1] # print(est_flow_charge_list[0][1]) # print(frame_treaty_no_list) frame_treaty_list = frame_treaty_get(frame_treaty_no_list) frame_treaty_eff_list = [] print(frame_treaty_list) # print(len(frame_treaty_list)) if len(frame_treaty_list) == 0: print('合约列表为空') else: for index in range(len(frame_treaty_list)): print(index) if dates >= str(frame_treaty_list[index][0][1]) and dates<= str(frame_treaty_list[index][0][2]): frame_treaty_eff_list.append(frame_treaty_list[index][0][0]) for index in range(len(frame_treaty_no_list)): if frame_treaty_no_list[index] in frame_treaty_eff_list: print(str(dates)+'日, '+str(frame_treaty_no_list[index])+'应产生预估流量费,一致') else: print(str(dates)+'日, '+str(frame_treaty_no_list[index])+'不应产生预估流量费,错误') for index in range(len(frame_treaty_eff_list)): if frame_treaty_eff_list[index] in frame_treaty_no_list: print(str(dates)+'日, '+str(frame_treaty_no_list[index])+'是存续期合约,已产生预估流量费,一致') else: print(str(dates)+'日, '+str(frame_treaty_no_list[index])+'是存续期合约,未产生预估流量费,错误') if __name__ == '__main__': dates = '2023-12-01' # dates = datetime.date.today() 获取当日日期 est_flow_charge_get(dates)
标签:est,no,--,frame,list,treaty,python,charge,debug From: https://www.cnblogs.com/lx001/p/17902940.html