user4434168
user4434168

Reputation:

python xlrd fetch first row value

I am using xlrd with python to pull excel data everything is fine and data pulling is fine but the code fetching last row data only.

Here is my Excel

+-------------------+------------+------------------------+
| polling_station_1 | Cairo      | 7734                   |
+-------------------+------------+------------------------+
| polling_station_2 | Giza       | 13332                  |
+-------------------+------------+------------------------+
| polling_station_3 | Alexandria | 10901                  |
+-------------------+------------+------------------------+

Here is my Code

import xlrd

excel_sheet = xlrd.open_workbook("teacher.xlsx")
sheet1= excel_sheet.sheet_by_name('parents')

for i in range(0, sheet1.nrows):        
      row = sheet1.row_slice(i)        
      Gname = row[0].value        
      Fname = row[1].value        
      Lname = row[2].value

print Gname
print Fname
print Lname

And the Result

polling_station_3  
Alexandria 
10901    

which is the last Row but i want data from first row Note : Changed Range still last row showing up.

Anyone Please Figure out

Upvotes: 1

Views: 11649

Answers (4)

Wolphyrus Imperius
Wolphyrus Imperius

Reputation: 868

If you don't want to iterate over the row, leave it to the xlrd module use the sheet method sheet.row_value(index) to get the values directly. Note that it's zero indexed.

Upvotes: 0

Noah
Noah

Reputation: 319

You have a coding issue. By changing the print statements, you can see the results.

import xlrd

excel_sheet = xlrd.open_workbook("teacher.xlsx")
sheet1= excel_sheet.sheet_by_name('parents')

for i in range(0, sheet1.nrows):        
    row = sheet1.row_slice(i)        
    Gname = row[0].value        
    Fname = row[1].value        
    Lname = row[2].value

    print Gname
    print Fname
    print Lname

You're iterating through the rows and only printing the final result currently.

Upvotes: 0

user4434168
user4434168

Reputation:

excel_sheet = xlrd.open_workbook("teacher.xlsx")
sheet1= excel_sheet.sheet_by_name('parents')
row = sheet1.row(0)  # 1st row

If print Row Output will be

[text:u'polling_station_1', text:u'cairo', text:u'7734']

Now slice it or get value one by one

Gname = row[0].value        
Fname = row[1].value        
Lname = row[2].value

print Gname
print Fname
print Lname 

Now Result will be

olling_station_3 
Alexandria 
10901    

If want to change row

row = sheet1.row(0)  # change row(0) Value

Upvotes: 1

Steve Misuta
Steve Misuta

Reputation: 1033

import xlrd

excel_sheet = xlrd.open_workbook("teacher.xlsx")
sheet1= excel_sheet.sheet_by_name('parents')

for i in range(0, sheet1.nrows):        
    row = sheet1.row_slice(i)        
    Gname = row[0].value        
    Fname = row[1].value        
    Lname = row[2].value

# these are outside the for loop so only printed for last row
print Gname
print Fname
print Lname

    # indent them to here and they will be printed for all rows
    print Gname
    print Fname
    print Lname        

Upvotes: 0

Related Questions