如何防止Python函数返回None

如何防止Python函数返回None,python,Python,我正在使用BeautifulSoup解析HTML表,如下所示: for tr in table_body.find_all('tr'): for td in tr: if td.text == 'Description': description = td.find_next('td').text if td.text == 'Category':

我正在使用BeautifulSoup解析HTML表,如下所示:

for tr in table_body.find_all('tr'):      
            for td in tr:  
                if td.text == 'Description':
                    description = td.find_next('td').text
                if td.text == 'Category':
                    category = td.find_next('td').text                             
                if td.text == 'Department':
                    department = td.find_next('td').text                             
                if td.text == 'Justification':
                    justification = td.find_next('td').text
print(description, category, department, justification)
for tr in table_body.find_all('tr'):      
            for td in tr:  
                description= html_check(td, 'Description')
                category = html_check(td, 'Category')
                department = html_check(td, 'Department')
                justification = html_check(td, 'Justification')
print(description, category, department, justification)
我将多个
if
语句重构为一个函数:

def html_check(td, text):
        if td.text == text:
            value = td.find_next('td').text
            return value
这就是所谓的:

for tr in table_body.find_all('tr'):      
            for td in tr:  
                if td.text == 'Description':
                    description = td.find_next('td').text
                if td.text == 'Category':
                    category = td.find_next('td').text                             
                if td.text == 'Department':
                    department = td.find_next('td').text                             
                if td.text == 'Justification':
                    justification = td.find_next('td').text
print(description, category, department, justification)
for tr in table_body.find_all('tr'):      
            for td in tr:  
                description= html_check(td, 'Description')
                category = html_check(td, 'Category')
                department = html_check(td, 'Department')
                justification = html_check(td, 'Justification')
print(description, category, department, justification)
我的问题是,当函数
html\u check
找不到匹配项时,它将返回
None
,并将打印出来。这是不可取的


有没有办法使此函数仅在满足if条件时才返回值?

如果退出函数调用时未指定返回值,Python将始终返回
None
。你的选择是:

  • 如果不满足条件,则返回其他内容
  • 如果函数返回
    None
选项1(不满足条件时返回其他内容):

选项2(如果返回
None
,则忽略该函数):


如果在退出函数调用时未指定返回,Python将始终返回
None
。你的选择是:

  • 如果不满足条件,则返回其他内容
  • 如果函数返回
    None
选项1(不满足条件时返回其他内容):

选项2(如果返回
None
,则忽略该函数):


您可以指定一个默认值,以在没有元素匹配的情况下返回。 比如:

def html_check(td, text):
        if td.text == text:
            value = td.find_next('td').text
            return value
        return "Default Value"
此外,您还可以通过参数指定默认值,该参数类似于:

 def html_check(td, text, default_value):
            if td.text == text:
                value = td.find_next('td').text
                return value
    return default_value
然后像这样使用它:

for tr in table_body.find_all('tr'):      
            for td in tr:  
                description= html_check(td, 'Description', 'Default Description')
                category = html_check(td, 'Category','Default Category')
                department = html_check(td, 'Department', 'Default Department')
                justification = html_check(td, 'Justification', 'Default Justification')
print(description, category, department, justification)

您可以指定一个默认值,以在没有元素匹配的情况下返回。 比如:

def html_check(td, text):
        if td.text == text:
            value = td.find_next('td').text
            return value
        return "Default Value"
此外,您还可以通过参数指定默认值,该参数类似于:

 def html_check(td, text, default_value):
            if td.text == text:
                value = td.find_next('td').text
                return value
    return default_value
然后像这样使用它:

for tr in table_body.find_all('tr'):      
            for td in tr:  
                description= html_check(td, 'Description', 'Default Description')
                category = html_check(td, 'Category','Default Category')
                department = html_check(td, 'Department', 'Default Department')
                justification = html_check(td, 'Justification', 'Default Justification')
print(description, category, department, justification)
我的解决办法是

def html_check(td, text):
        if td.text == text:
            value = td.find_next('td').text
            if not value is None:
               return value
            else:
                value ="Not able to find"
                return value
然而,我们不能删除None,如果html_check函数没有返回任何内容,那么在python中,我们使用None初始化它。但是为了我们的利益,我们可以绕过它,并用开发人员想要的其他东西初始化它。

我的解决方案是

def html_check(td, text):
        if td.text == text:
            value = td.find_next('td').text
            if not value is None:
               return value
            else:
                value ="Not able to find"
                return value

然而,我们不能删除None,如果html_check函数没有返回任何内容,那么在python中,我们使用None初始化它。但是为了我们的利益,我们可以绕过它,并用开发人员想要的其他东西初始化它。

您可以尝试只打印那些值匹配的内容

for tr in table_body.find_all('tr'): 
            fields = ['Description','Category','Department','Justification']
            for td in tr:
                print (['{}:{}'.format(i,html_check(td,i)) for i in fields if html_check(td,i)])

您可以尝试仅打印其值匹配的值

for tr in table_body.find_all('tr'): 
            fields = ['Description','Category','Department','Justification']
            for td in tr:
                print (['{}:{}'.format(i,html_check(td,i)) for i in fields if html_check(td,i)])

当条件不满足时,应该怎么办?返回一个空字符串?如果是-只需在函数末尾添加
return”“
,超出
If
条件。如果没有从函数显式返回任何内容,将返回
None
None
从函数返回的内容不会自动打印。只需使用另一个
if
检查函数是否返回
None
,当它返回除
None
以外的内容时,是否仅返回
print()
“是否有办法使此函数仅在满足if条件时返回值?”不。如果不为给定的输入指定返回值,Python将使用函数return
None
。当不满足条件时,我希望函数的行为也像从未调用过一样-因此不返回任何内容,即使
None
Python也不会这样工作。您可以忽略
None
,但不能阻止解释器返回它。只需有条件地调用函数。当条件不满足时会发生什么?返回一个空字符串?如果是-只需在函数末尾添加
return”“
,超出
If
条件。如果没有从函数显式返回任何内容,将返回
None
None
从函数返回的内容不会自动打印。只需使用另一个
if
检查函数是否返回
None
,当它返回除
None
以外的内容时,是否仅返回
print()
“是否有办法使此函数仅在满足if条件时返回值?”不。如果不为给定的输入指定返回值,Python将使用函数return
None
。当不满足条件时,我希望函数的行为也像从未调用过一样-因此不返回任何内容,即使
None
Python也不会这样工作。您可以忽略
None
,但不能阻止解释器返回它。只需有条件地调用该函数。您的解决方案(如当前提供的)仍将返回
None
。您可以分配
value=“无法找到”
,但不返回它。这意味着它仍然返回
None
Ohh我忘了添加一些行。我的代码没有经过优化,这只是一个例子,我们可以优化代码,以便只调用一次return。您的解决方案(如当前提供的)仍将返回
None
。您可以分配
value=“无法找到”
,但不返回它。这意味着它仍然返回
None
Ohh我忘了添加一些行。我的代码没有经过优化,只是一个例子而已,我们可以对代码进行优化,以便只调用一次return。