0
import xlrd
file_location = "/home/myuser/excel.xls"
workbook = xlrd.open_workbook(file_location)
sheet = workbook.sheet_by_index(0)
data = [[sheet.cell_value(r, c) for c in range(sheet.ncols)] for r in range(sheet.nrows)]

for r in data:
    print r[1]

i want to get which row in excel is iterating in the for loop, Can anybody help me regarding this.

Srikanth
  • 65
  • 1
  • 1
  • 7
  • I'm not sure what you are asking. What does your code currently do? What do you expect it to do? – Zev Jun 11 '18 at 12:30
  • Can you provide more information on what _get which row in excel is iterating in the for loop_ means. – scharette Jun 11 '18 at 12:30
  • Duplicate of https://stackoverflow.com/questions/522563/accessing-the-index-in-for-loops – Novaterata Jun 11 '18 at 12:32
  • I need to handle the rows in xlsx based on iteration count. using r[1] inside for loop i am able to get 1st column value in 1st row. now i need after few iterations, how to get which row is currently iterating and if i want to get previous iteration values. how can get them in this way. – Srikanth Jun 11 '18 at 12:47

1 Answers1

1

Use enumerate:

for i, r in enumerate(data):
    print 'current row is {}'.format(i)
    print r[1]

If you want to count from 1 just use enumerate(data, 1).

zipa
  • 27,316
  • 6
  • 40
  • 58
  • thanks, if i want to get previous iteration values ex : if i want to get previous row value how can i get. – Srikanth Jun 11 '18 at 12:59
  • @Srikanth well You can subtract `1` but remember that there is no previous value for the first, so you'd have to figure out what you want in that case – zipa Jun 11 '18 at 13:08
  • i want previous value after first iteration, suppose if i want to get text in 2nd row column 1&2, i need to check 1st row columns 1&2 values in 2nd iteration. how to solve that. i have tried with subtraction but i did not get previous values. – Srikanth Jun 11 '18 at 13:11
  • @Srikanth Given that you seem to have a question there, why don't you write that in another question, include example of input and desired output, and you might get awesome answers? – zipa Jun 11 '18 at 13:12
  • I need to get row count and particular column values using python list iteration for every row in excel i need to get username and password. i have used the below code. xl_workbook = xlrd.open_workbook(file) sheet = xl_workbook.sheet_by_index(0) vendor = [[sheet.cell_value(r,c) for c in range(sheet.ncols)] for r in range(1,sheet.nrows)] for i in vendor: user_name = i[1] password=i[2] like this when first iteration is completed, i need to check previous username and password and need to compare with 2nd iteration values. – Srikanth Jun 11 '18 at 13:22