Python openpyxl - indexing columns that are not empty

时间:2019-02-24 03:13:14

标签: python loops openpyxl

I am a novice programmer in Python. The problem seems to be that there is a loop that never terminates, but I can't understand why.

I am using the openpyxl library in Python to read (and, later on, sort) data from an Excel file (.xlsx).

The first step in the data analysis is to count and index the number of columns that are not empty. To do that, I am using the following code:

#First creating a vector/list to index the non-empty columns:
filled_column_indexer = []
#Looping over columns and rows to identify filled columns:
column_indexer = 1
while column_indexer < sheet.max_column + 1:
    row_indexer = 1
    while row_indexer < sheet.max_row + 1:
        if sheet.cell(row = row_indexer, column = column_indexer).value == None:
            row_indexer = row_indexer + 1
        else:
            filled_column_indexer = filled_column_indexer + [column_indexer]
            break
for elem in filled_column_indexer:
    print(elem)

The idea is to use loops to see whether a column contains any cells that contain data. However, the nested loop that I am using never terminates for some reason.

Please keep in mind that I would prefer that any solution not use excessive syntactical sugar; I am still a novice programmer and I need to see exactly what is happening.

Edit: Has been answered sufficiently.

0 个答案:

没有答案