繁体   English   中英

如何防止Python function返回None

[英]How to prevent Python function from returning None

我正在像这样用 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)

我将多个if语句重构为 function:

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:  
                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)

我的问题是,当 function html_check找不到匹配项时,它将返回None ,这将被打印出来。 这是不可取的。

有什么办法可以让这个function只有满足其中的if条件才返回一个值吗?

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

  • 如果不满足条件,则返回其他内容。
  • 如果函数返回None则忽略该函数

选项 1(不满足条件时返回其他内容):

 def html_check(td, text):
     if td.text == text:
        value = td.find_next('td').text
        return value
     return "no value found"

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

if html_check(td, 'section'):
     # do things

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

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 初始化它。 但是为了我们的缘故,我们可以绕过并使用开发人员想要的其他东西来初始化它。

您可以尝试仅打印值匹配的那些。

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)])

仅在所有变量都有值时打印:

print_me = description and category and department and justification
print(description, category, department, justification) if print_me else None

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM