代码之家  ›  专栏  ›  技术社区  ›  barciewicz

如何防止python函数返回none

  •  0
  • barciewicz  · 技术社区  · 6 年前

    我正在用这样漂亮的汤解析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 语句转换为函数:

    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)
    

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

    只有当函数中的if条件满足时,才能使该函数返回值吗?

    4 回复  |  直到 6 年前
        1
  •  5
  •   Christian W.    6 年前

    巨蟒总会回来 None ,如果在退出函数调用时未指定返回值。您的选择是:

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

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

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

    选项2(如果 没有 返回):

    if html_check(td, 'section'):
         # do things
    
        2
  •  0
  •   Prakhar Agnihotri    6 年前

    如果没有匹配的元素,可以指定要返回的默认值。 类似:

    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)
    
        3
  •  0
  •   satyam soni    6 年前

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

    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)])
    
        4
  •  0
  •   TheExorcist    6 年前

    我的解决办法是

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