How to get line number in excel sheet using python?

三世轮回 提交于 2019-12-18 19:40:25

问题


I have an Excel sheet like this:

  A B C D E F G
1 0 0 0
2   0 0
3 0
4
5 0 0 0 0 0 0 0
6 0 0   0

And every '0' represents some data values, I am reading the values and comparing them with my original data, when the data match fails it returns a message. What I want is to return the line number of excel sheet as well as the column number or particularly the CELL location. Please help me doing this!


回答1:


Try this:

import xlrd
workbook = xlrd.open_workbook('book.xls')
for sheet in workbook.sheets():
    for row in range(sheet.nrows):
        for column in range(sheet.ncols):
            print "row::::: ", row
            print "column:: ", column
            print "value::: ", sheet.cell(row,column).value

It will give you exact row, column numbers and cell value




回答2:


Keep track of the column and row while iterating over the worksheet. An xlrd Cell does not keep track of where they 'live' in the sheet.




回答3:


if you are comparing cell values then you have their index which is line number and column number, otherwise you can start counter on row and column loop with increment on every iteration, this will tell you exact column number and row number.



来源:https://stackoverflow.com/questions/15541641/how-to-get-line-number-in-excel-sheet-using-python

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!