'How to prevent Python function from returning None

I am parsing HTML table with BeautifulSoup like this:

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)

I refactored the multiple if statements into a function:

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

that is called like this:

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)

My problem is that when the function html_check will not find a match, it will return None, which will be printed. This is not desirable.

Is there any way to make this function return a value only when the if condition in it is met?



Solution 1:[1]

Python will always return None, if no return is specified at the point of exiting the function call. Your options are:

  • return something else if the condition is not met.
  • ignore the function if it returns None

option 1 (return something else when the condition isn't met):

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

option 2 (ignores the function if None is returned):

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

Solution 2:[2]

You can specify a default value, to return, in case no element matches. Something like:

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

Also, you can spcify the default value via argument, which would somewhat look like :

 def html_check(td, text, default_value):
            if td.text == text:
                value = td.find_next('td').text
                return value
    return default_value

And, then use it like:

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)

Solution 3:[3]

You can try this to print only those whose values match.

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

Solution 4:[4]

My solution would be

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

However we can not remove None, if html_check function is not returning anything, in python we init it with None. But for our sake we can bypass and init it with something else dev wants.

Solution 5:[5]

Only print if all variables have a value:

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

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2 Prakhar Agnihotri
Solution 3 satyam soni
Solution 4
Solution 5 BeRT2me