简体   繁体   中英

how to count number of filled cells in each column of excel using python

I have a sheet in Excel which is not structured data as you can see here .

I tried using xlrd package

total_rows=worksheet.nrows
total_cols=worksheet.ncols

but this is giving me the maximum row count for a column. But I need filled cell count for each column. Could anyone suggest solution.

You can try to iterate through every row and check whether the cell is empty or not. You can use this:

import xlrd
book = xlrd.open_workbook('excel.xls')
sheet = book.sheet_by_index(0)

count = 0
for row in range(sheet.nrows):
    for col in sheet.row_values(row):
        if col.strip() != '':
            count += 1

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM