我构建了一个函数来创建字典并返回它。此函数称为get_values
,其结构如下:
def initiate_values(directory):
for file in glob.glob(os.path.join(directory, '*.[xX][lL][sS]')):
title = os.path.basename(file).lower()
if title == 'etc.xls':
wb = xlrd.open_workbook(file)
wb = wb.sheet_by_name(u'Sheet1')
get_values(file, wb)
def get_values():
info_from_etc = dict()
# build dict
return info_from_etc
它可以工作,因为它创建字典,然后当我尝试打印它时,它会打印正确的值。但是,当我尝试从另一个函数调用这个get_values
函数时,字典返回为“None”。这是我调用get_values
-
我在这里做了什么不正确的事情,我如何在这里返回正确的字典——也就是说,不是None
的字典。在
您需要
return
来自initiate_values
的字典:initiate_values
不返回任何内容,因此在Python默认情况下,它返回None
。您应该能够根据您要执行的操作来确定将return语句放在何处。在我同意您确实需要在initiate_values()函数中返回字典,但是在initiate_values函数中也给get_values两个参数(file,wb),并且在声明中没有给它任何参数。似乎那里也有问题。在
相关问题 更多 >
编程相关推荐