I have recently started learning PYTHON and I am facing some difficulty.
I am trying to extract data from excel sheet with and without def statement . So without def, I am getting valid output but when I extract data using def statement, I am getting "NONE" as output. Please help me to resolve this.
book=xlrd.open_workbook("E:\\Email.xlsx")
print(book.nsheets)
SheetName =book.sheet_by_name("gmail")
rows=SheetName.nrows
cols=SheetName.ncols
print(rows)
print(cols)
##### WITHOUT DEFINING ANY FUNCTION ####
table=list()
record=list()
for x in range(rows):
for y in range(cols):
record.append(SheetName.cell(x,y).value)
table.append(record)
record=[]
rows=rows+1
print(table)
WITH DEFO/P:[['Email ID:'], ['AVVC.com'], ['Password'], ['abcdefg']]
def getcell(rows,cols): table=list() record=list() for x in range(rows): for y in range(cols): record.append(SheetName.cell(x,y).value) table.append(record) record=[] rows=rows+1 return; v= getcell(0,1) print(v)
O/P:NONE
When you call return
in a function
it stops the execution and passes back whatever is after it.
As you had your return
with nothing after it, it was returning None
(nothing). What you want to do is return the table
list
so that it works in the same way as when there wasn't a function
. Also, as return
stops the execution, it needs to be at the very end of your function
- after the for-loops
so that they can complete.
Making your function
:
def getcell(rows,cols):
table=list()
record=list()
for x in range(rows):
for y in range(cols):
record.append(SheetName.cell(x,y).value)
table.append(record)
record=[]
rows=rows+1
return table
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.