Search code examples
pythonexcelxlrd

How to print a row with blank columns omitted?


I'm trying to print a row of headers from Excel, but some of the fields are blank, even though the column itself has data entries.
I only want to print the columns that don't have a blank header. Right now I'm just working on trying to get the program to print headers that aren't blank, but I can't figure out how.
I am a Python beginner so a lot of this stuff doesn't seem obvious to me.

Here's my code for just trying to get the headers:

import xlrd
import xlwt

book = xlrd.open_workbook("file.xlsx")
sheet = book.sheets()[0]
r = sheet.row(0)

print(r)

That prints every value in the row, but the blank ones come up as , empty:, and those are the ones I want to exclude.

When I try this bit to only print non-empty rows:

for row in range(r):
    if r is not None:
        print(r)

I get an error that a 'list' object can't be interpreted as an integer.


Solution

  • The range object in Python is an object that creates an iterable range of integers. E.g. you can do for i in range(100): and the loop will iterate over each integer between 0 and 100.

    The row function already returns a sequence of Cell objects, which is iterable by itself. To iterate over this, just do for cell in row:.

    The objects in a row are never None, so checking row is not None will not eliminate the empty cells. Empty cells are singletons, that is, there is only ever one instance of an empty cell, all other instances are not empty. The correct way to check if a cell is empty is:

    from xlrd.sheet import empty_cell
    
    if cell is empty_cell:
    

    The verbose way to accomplish what you want is this:

    for cell in row:
        if cell is not empty_cell:
            print cell
    

    Using list comprehension, this can be simplified to a single line:

    print [cell for cell in row if cell is not empty_cell]