如何使用python获取excel表中的行号?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15541641/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to get line number in excel sheet using python?
提问by MHS
I have an Excel sheet like this:
我有一个这样的 Excel 工作表:
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!
每个“0”代表一些数据值,我正在读取这些值并将它们与我的原始数据进行比较,当数据匹配失败时,它返回一条消息。我想要的是返回 Excel 表的行号以及列号或特别是 CELL 位置。请帮我做这个!
采纳答案by arulmr
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, columnnumbers and cellvalue
它会给你确切的row,column号码和cell值
回答by Anthon
Keep track of the column and row while iterating over the worksheet. An xlrdCelldoes not keep track of where they 'live' in the sheet.
在迭代工作表时跟踪列和行。AnxlrdCell不会跟踪它们在工作表中“居住”的位置。
回答by sharafjaffri
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.
如果您正在比较单元格值,那么您有它们的索引,即行号和列号,否则您可以在行和列循环上启动计数器,每次迭代都会增加,这将告诉您确切的列号和行号。

