我的迭代代码出了什么问题

时间:2015-12-10 07:58:45

标签: python list loops openpyxl

请投票支持我的问题我刚被禁止在关键时刻向我发布问题。我是初学程序员。请大家。

我有两个列表,我需要遍历两个列表中的每个项目(按顺序)以使用openpyxl更改Excel单元格填充颜色。我在以ws.cell(row = int(items),column

开头的行中出现语法错误

以下是代码:

color_lst= ['FF333399', 'FFFFFF00', 'FFC0C0C0', 'FF00FF00', 'FF008080', 'FF00FFFF', 'FF000080', 'FF800000', 'FF969696', 'FFFF99CC', 'FFFFFF99', 'FF800080', 'FF99CC00', 'FF0000FF']

label_list_for_col_header= ['Energy', 'Green Buildings', 'High Performance Buildings', 'Computing', 'Design', 'Infrared', 'Laser scanning', 'Outdoor Thermal Comfort', 'Urban Desgin', 'Daylighting', 'Thermal Comfort', 'Parametric', 'Simulation Tools', 'N']

gerenral_lst= list()

m= 0    
for row in ws.iter_rows('D2:D11'):
    for cell in row:
        if label_list_for_col_header[m] in cell.value : 
            general_lst.append(cell.row)
        for items in range(len(general_lst)):

            ws.cell(row = int(items), column = int(label_list_for_col_header.index(label_list_for_col_header[0])).fill = PatternFill(start_color=str(color_lst[m]), end_color=str(color_lst[m]) , fill_type='solid')
    general_lst = []
    m +=1       

我根据建议修改了代码,但是循环只执行了一次。不确定为什么

m= 0    
for row in ws.iter_rows('D2:D11'):
    for cell in row:
        if label_list_for_col_header[m] in cell.value : 
            general_lst.append(cell.row)
        for items in range(len(general_lst)):

            ws.cell(row = general_lst[items], column = int(m+5)).fill = PatternFill(start_color=str(color_lst[m]), end_color=str(color_lst[m]) , fill_type='solid')
    general_lst = []
    m +=1       

我终于得到了代码,以防其他人遇到同样的问题,这是工作代码:

m= 0    
for lbls in label_list_for_col_header: 
    j= int(label_list_for_col_header.index(lbls))+5
    for row in ws.iter_rows('D2:D11'):
        for cell in  row:

            if lbls in cell.value : 
                general_lst.append(cell.row)
                for items in range(len(general_lst)):

                    ws.cell(row = general_lst[items], column = j).fill = PatternFill(start_color=str(color_lst[m]), end_color=str(color_lst[m]) , fill_type='solid')
    general_lst = []
    m +=1       

2 个答案:

答案 0 :(得分:1)

参考编辑错误:

您可以通过以下方式迭代列表:

/* Prepare the intent to create a new event */
Intent intent =
    new Intent(Intent.ACTION_INSERT)
        .setData(CalendarContract.Events.CONTENT_URI)
        .putExtra(
            CalendarContract.EXTRA_EVENT_BEGIN_TIME,
            startDate.getTimeInMillis())
        .putExtra(
            CalendarContract.EXTRA_EVENT_END_TIME,
            endDate.getTimeInMillis())
        /* This part doesn't work */
        .putExtra(
            CalendarContract.Events.CALENDAR_ID,
            m_datasource.getMyCalendarID());

/* ...and start the activity */
startActivityForResult(intent, Mode.Creating.getValue());

无需for items in general_lst:

答案 1 :(得分:1)

尝试尽可能多地保留循环中的过程代码非常重要。这使得它更容易理解,并且可能更快。因此,例如,您可以提前列出所有可能的填充。

猜测你想做什么我认为以下代码可能有效或足够接近。提供尽可能多的上下文非常重要,即。 ws是什么。

from openpyxl import load_workbook
wb = load_workbook("List of books about buildings.xlsx")
ws = wb.active

color_lst= ['FF333399', 'FFFFFF00', 'FFC0C0C0', 'FF00FF00', 'FF008080', 'FF00FFFF', 'FF000080', 'FF800000', 'FF969696', 'FFFF99CC', 'FFFFFF99', 'FF800080', 'FF99CC00', 'FF0000FF']

label_list_for_col_header= ['Energy', 'Green Buildings', 'High Performance Buildings', 'Computing', 'Design', 'Infrared', 'Laser scanning', 'Outdoor Thermal Comfort', 'Urban Desgin', 'Daylighting', 'Thermal Comfort', 'Parametric', 'Simulation Tools', 'N']

fills = [PatternFill(fgColor=color), bgColor=color, fill_type="solid") for color in color_lst]


for idx, row in enumerate(ws.iter_rows('D2:D11')):
    for cell in row:
        if label_list_for_col_header[idx] in cell.value : 
            cell.fill = fills[idx]