Home > Mobile >  How to enclose loops into function in python?
How to enclose loops into function in python?

Time:12-19

How to write function of this loop, I want to convert this loop into def fucntion how can I write it

 for row_i, row in enumerate(ws.iter_rows()):
        data_row = []
        # data_list.append(data_row)
        for col_i, cell in enumerate(row):
            row_dim = ws.row_dimensions[cell.row]
            if cell.coordinate in excluded_cells or row_dim.hidden:
                continue

            cell_data = {
                "id": "",
                "row": cell.row,
                "col": cell.column,
                "row_span": 1,
                "col_span": 1,
                "label": "",
                "xmin": "",
                "ymin": "",
                "xmax": "",
                "ymax": "",
                "score": "",
                "text": "" if cell.value == None else cell.value,
                "rowlabel": "",
                "verification_status": "",
                "status": "",
                "failed_validation": "",
                "label_id": "",
                "Merged_cell": None,
            }
            merged_cell_info = merged_cell_map.get(
                cell.coordinate, {"attrs": {"row_span": 1, "col_span": 1}}
            )
            if merged_cell_info:
                cell_data["row_span"] = merged_cell_info["attrs"]["row_span"]
                cell_data["col_span"] = merged_cell_info["attrs"]["col_span"]
                cell_data["Merged_cell"] = (
                    False
                    if (cell_data["row_span"] <= 1 and cell_data["col_span"] <= 1)
                    else True
                )
            if col_i   cell_data["col_span"] > total_col:
                total_col = col_i   cell_data["col_span"]

            if row_i   cell_data["row_span"] > total_row:
                total_row = row_i   cell_data["row_span"]

            data_list.append(cell_data)

I want to write this loop into def function and call it at main file, how to write function from this

CodePudding user response:

just put it inside a function ant pass the necessary parameters to that function

def my_function(param1, param2):
   # your loops and rest of the code

Then you can cann the function, and it will execute the loops inside

my_function(param1, param2)

CodePudding user response:

This is one approach you can write a function using this loop:

def process_worksheet(ws, merged_cell_map, excluded_cells, total_col=0, total_row=0):
    data_list = []
    for row_i, row in enumerate(ws.iter_rows()):
        data_row = []
        # data_list.append(data_row)
        for col_i, cell in enumerate(row):
            row_dim = ws.row_dimensions[cell.row]
            if cell.coordinate in excluded_cells or row_dim.hidden:
                continue

            cell_data = {
                "id": "",
                "row": cell.row,
                "col": cell.column,
                "row_span": 1,
                "col_span": 1,
                "label": "",
                "xmin": "",
                "ymin": "",
                "xmax": "",
                "ymax": "",
                "score": "",
                "text": "" if cell.value == None else cell.value,
                "rowlabel": "",
                "verification_status": "",
                "status": "",
                "failed_validation": "",
                "label_id": "",
                "Merged_cell": None,
            }
            merged_cell_info = merged_cell_map.get(
                cell.coordinate, {"attrs": {"row_span": 1, "col_span": 1}}
            )
            if merged_cell_info:
                cell_data["row_span"] = merged_cell_info["attrs"]["row_span"]
                cell_data["col_span"] = merged_cell_info["attrs"]["col_span"]
                cell_data["Merged_cell"] = (
                    False
                    if (cell_data["row_span"] <= 1 and cell_data["col_span"] <= 1)
                    else True
                )
            if col_i   cell_data["col_span"] > total_col:
                total_col = col_i   cell_data["col_span"]

            if row_i   cell_data["row_span"] > total_row:
                total_row = row_i   cell_data["row_span"]

            data_list.append(cell_data)
    return data_list

To use this function in your main file, you can call it like this:

data_list = process_worksheet(ws, merged_cell_map, excluded_cells)

  • Related