python - How to remove empty Excel line after loop exception? -


i short before finishing script , trying head around this.

i have script puts information array (data) excel lines via loop. data_loaded array script entries. looks this:

    get_comp_data import get_comp_data     openpyxl import workbook     wb = workbook()     ws = wb.active             z in range(len(data_loaded)):                 try:                     data = get_comp_data(data_loaded[z])                     print z                     ws['a'+str(z+1)] = data[0]                     ws['b'+str(z+1)] = data[1]                     ws['c'+str(z+1)] = data[2]                     ws['d'+str(z+1)] = data[3]                     ws['e'+str(z+1)] = data[4]                     ws['f'+str(z+1)] = data[5]                     ws['g'+str(z+1)] = data[6]                     ws['h'+str(z+1)] = data[7]                     ws['i'+str(z+1)] = data[8]                     ws['j'+str(z+1)] = data[9]                     wb.save("test.xlsx")                 except exception:                     pass 

however, exception raised in case get_comp_data gets error. every time exception raised excel line empty.

how can delete or prevent these lines being created?

z counted regardless of exception occurring or not rows skipped. code has written in way row number incremented when there no error , row got filled.

iterating on index values of sequence instead of items ”unpythonic” anyway.

from string import ascii_uppercase get_comp_data import get_comp_data openpyxl import workbook  workbook = workbook() worksheet = workbook.active  # ...  row_number = 1 item in data_loaded:     try:         data = get_comp_data(item)     except exception:         pass  # intentionally ignored.     else:         column_name, item in zip(ascii_uppercase, data):             worksheet[column_name + str(row_number)] = item         row_number += 1 

Comments