预计会出现缩进块错误

时间:2015-11-03 05:42:45

标签: python excel text

我有一张4行5列的Excel表格。最后一列具有两个值中的任何一个,即。已完成并待定。我想要的是将具有最后一列值的行导出为文本文件。我试过一个代码,但它给出了错误

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")

1 个答案:

答案 0 :(得分:0)

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

    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()