if / elif / else语句-似乎无法获得所需的输出

时间:2019-03-06 21:51:42

标签: python-3.x

如果input_month =“ jan”脚本可以正常工作,但是如果input_month =其他月份,则该脚本似乎不会执行任何elif块,而是直接进入else并通过。

(这是使用openpyxl模块加载/写入excel文件的,不确定是否需要提及)

此外,有什么方法可以将其重构为更整洁的东西吗?我不是经验丰富的程序员,但我觉得这不是执行此操作的最佳方法。

if x in reporting:
    wb = load_workbook('report.xlsx')
    ws = wb[f'Rep{x}']

    wb['Summary']['B3'] = month_year

    ws['C5'] = terr_dets[f'Rep{x}'][1]
    ws['D5'] = terr_dets[f'Rep{x}'][3]
    ws['E5'] = terr_dets[f'Rep{x}'][2]
    ws['C6'] = terr_dets[f'Rep{x}'][0]
    ws['C6'].number_format = '#,##0.00'

    if (input_month == "jan"):
        ws['C22'] = totals_rep_dict[f"totals_rep{x}"] #january
        ws['C22'].number_format = '#,##0.00'
    elif (input_month == "feb"):
        ws['C23'] = totals_rep_dict[f"totals_rep{x}"] #february
        ws['C23'].number_format = '#,##0.00'
    elif (input_month == "mar"):
        ws['C24'] = totals_rep_dict[f"totals_rep{x}"] #march
        ws['C24'].number_format = '#,##0.00'
    elif (input_month == "apr"):
        ws['C25'] = totals_rep_dict[f"totals_rep{x}"] #april
        ws['C25'].number_format = '#,##0.00'
    elif (input_month == "may"):
        ws['C26'] = totals_rep_dict[f"totals_rep{x}"] #may
        ws['C26'].number_format = '#,##0.00'
    elif (input_month == "jun"):
        ws['C27'] = totals_rep_dict[f"totals_rep{x}"] #june
        ws['C27'].number_format = '#,##0.00'
    elif (input_month == "jul"):
        ws['C28'] = totals_rep_dict[f"totals_rep{x}"] #july
        ws['C28'].number_format = '#,##0.00'
    elif (input_month == "aug"):
        ws['C29'] = totals_rep_dict[f"totals_rep{x}"] #august
        ws['C29'].number_format = '#,##0.00'
    elif (input_month == "sep"):
        ws['C30'] = totals_rep_dict[f"totals_rep{x}"] #september
        ws['C30'].number_format = '#,##0.00'
    elif (input_month == "oct"):
        ws['C31'] = totals_rep_dict[f"totals_rep{x}"] #october
        ws['C31'].number_format = '#,##0.00'
    elif (input_month == "nov"):
        ws['C32'] = totals_rep_dict[f"totals_rep{x}"] #november
        ws['C32'].number_format = '#,##0.00'
    elif (input_month == "dec"):
        ws['C33'] = totals_rep_dict[f"totals_rep{x}"] #december
        ws['C33'].number_format = '#,##0.00'
    else:
        pass

    wb.save('Sales Analysis ' + month_year + '.xlsx')

1 个答案:

答案 0 :(得分:0)

谢谢你的想法!

我能够使它与字典配合使用;我阅读了python中的case / switch语句,并试图构建一个函数,该函数在那里获得了90%的性能,但是不断出现迭代错误,我将继续尝试并调试。

缺少的细节似乎是“ .value”

我尝试对.number_format执行相同的操作,但以下代码无效:

report_dict[input_month].value.number_format = '#,##0.00'

以下是当前正在执行的操作:

wb = load_workbook('report.xlsx')
wb['Summary']['B3'] = month_year

for x in repnumbers:
    if x in reporting:
        ws = wb[f'Rep{x}']
        ws['C5'] = terr_dets[f'Rep{x}'][1]
        ws['D5'] = terr_dets[f'Rep{x}'][3]
        ws['E5'] = terr_dets[f'Rep{x}'][2]
        ws['C6'] = terr_dets[f'Rep{x}'][0]
        ws['C6'].number_format = '#,##0.00'
        report_dict = {"jan": ws['C22'],
                "feb": ws['C23'],
                "mar": ws['C24'],
                "apr": ws['C25'],
                "may": ws['C26'],
                "jun": ws['C27'],
                "jul": ws['C28'],
                "aug": ws['C29'],
                "sep": ws['C30'],
                "oct": ws['C31'],
                "nov": ws['C32'],
                "dec": ws['C33']
        } 
        report_dict[input_month].value = totals_rep_dict[f"totals_rep{x}"]
        # report_dict[input_month].value.number_format = '#,##0.00'
        ws['C22'].number_format = '#,##0.00'
        ws['C23'].number_format = '#,##0.00'
        ws['C24'].number_format = '#,##0.00'
        ws['C25'].number_format = '#,##0.00'
        ws['C26'].number_format = '#,##0.00'
        ws['C27'].number_format = '#,##0.00'
        ws['C28'].number_format = '#,##0.00'
        ws['C29'].number_format = '#,##0.00'
        ws['C30'].number_format = '#,##0.00'
        ws['C31'].number_format = '#,##0.00'
        ws['C32'].number_format = '#,##0.00'
        ws['C33'].number_format = '#,##0.00'
        wb.save('Sales Analysis ' + month_year + '.xlsx')