简体   繁体   中英

expected an indented block error

I have an excel sheet having 4 rows and 5 columns. The last columns has either of the two values ie. completed and pending. All I want is to export the rows having their last column value as pending to a text file. I have tried a code but it is giving error

import xlrd

workbook=xlrd.open_workbook('C:/Users/admin/Documents/omkar.xlsx')
sh=workbook.sheet_by_name('Sheet1')

def ncols():

print sh.nrows
print sh.ncols

n=0
i=0

file=open('C:/Users/admin/Documents/om.txt','w')

for n in range(sh.nrows):

    for i in range(sh.ncols):
        data =str(sh.cell_value(n,i)) + " "

    if  sh.cell_value(n,ncols) == "pending":

        print  data,
        file.write(data + " ")

    print 
    file.write("\n")
import xlrd

workbook=xlrd.open_workbook('C:/Users/admin/Documents/omkar.xlsx')
sh=workbook.sheet_by_name('Sheet1')

def ncols():

print sh.nrows
print sh.ncols

n=0
i=0

file=open('C:/Users/admin/Documents/om.txt','w')

for n in range(sh.nrows):

    for i in range(sh.ncols):
        data =str(sh.cell_value(n,i)) + " "

    if  sh.cell_value(n,ncols) == "pending":

        print  data,
        file.write(data + " ")

    print 
    file.write("\n")

should be-

import xlrd

workbook=xlrd.open_workbook('C:/Users/admin/Documents/omkar.xlsx')
sh=workbook.sheet_by_name('Sheet1')

def ncols():
    print sh.nrows
    print sh.ncols

    file = open('C:/Users/admin/Documents/om.txt', 'w')

    for n in range(sh.nrows):
        if sh.cell_value(n, sh.ncols-1) == 'pending':
            for i in range(sh.ncols):
                data = str(sh.cell_value(n, i)) + ' '
                print data,
                file.write(data)
            print
            file.write('\n')

ncols()

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