【发布时间】:2017-01-30 12:22:45
【问题描述】:
我正在调用该函数,而 a 没有保留下面函数中的值,即 mob_f returned:
a = get_mobile()
函数是:
def get_mobile():
ws = sheet_select()
mobile = []
mob_i = []
mob_f = []
mob_j = []
for col in ws.iter_cols():
for cell in col:
if cell.value == 'Mobile':
x=column_index_from_string(cell.column)
for row in ws.iter_rows(min_col = x, min_row = 2, max_col = x):
for cell in row:
if cell.value != None:
mobile.append(cell.value)
for i in mobile:
h = i
h = h.replace(" ", "")
h = h.replace("+(91)-", ",")
h = h.replace("+91", "")
h = h.replace("-", "")
mob_i.append(h)
for i in mob_i:
h = i
h = h.split(',')
mob_j.append(h)
mob_x = [item for sublist in mob_j for item in sublist]
for i in mob_x:
if i != '':
h = i
return mob_f.append(h)
如果我使用代码而不将其定义为function,它运行没有问题,我得到mob_f。
我认为问题是 return 放置不正确。我尝试了很多组合并且一直失败。昨晚同样的功能也在工作,我不明白我哪里出错了。
【问题讨论】:
-
可以包含excel文件吗?
-
@MYGz,excel文件打开等运行顺利。如果您认为需要,我会包含它。
-
是的。如果可以的话,包括它。它提供了更好的理解。
-
@MyGz Maurice 在下面的回答让我明白了。不过还是谢谢。
-
好的。 NP。最好在代码中包含输入和输出以获得更好的建议。
标签: python python-3.x user-defined-functions