我想在python中使用和不使用def语句(函数)从excel表中提取数据

时间:2022-06-18 05:17:02

I have recently started learning PYTHON and I am facing some difficulty.

我最近开始学习PYTHON,我面临一些困难。

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.

我试图从有和没有def语句的excel表中提取数据。所以没有def,我得到有效的输出但是当我使用def语句提取数据时,我得到“NONE”作为输出。请帮我解决这个问题。

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)

O/P:[['Email ID:'], ['AVVC.com'], ['Password'], ['abcdefg']]

O / P:[['电子邮件ID:'],['AVVC.com'],['密码'],['abcdefg']]

WITH DEF
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

O / P:NONE

1 个解决方案

#1


0  

When you call return in a function it stops the execution and passes back whatever is after it.

当你在一个函数中调用return时,它会停止执行并返回它之后的任何内容。

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.

因为你没有任何回报,它返回无(没有)。你想要做的是返回表列表,使其工作方式与没有函数时相同。此外,当返回停止执行时,它需要位于函数的最后 - 在for循环之后,以便它们可以完成。

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

#1


0  

When you call return in a function it stops the execution and passes back whatever is after it.

当你在一个函数中调用return时,它会停止执行并返回它之后的任何内容。

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.

因为你没有任何回报,它返回无(没有)。你想要做的是返回表列表,使其工作方式与没有函数时相同。此外,当返回停止执行时,它需要位于函数的最后 - 在for循环之后,以便它们可以完成。

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