PythScript outputs "None" at...

fb16
2012-06-03
2012-11-14
  • fb16
    fb16
    2012-06-03

    Hello,

    This script returns a prettyfied version of an HTML document.

    Why it outputs also the string

    None
    

    at the beginning of the document?

    Thank you

    from bs4 import BeautifulSoup, Comment
    def _remove_attrs(soup):
        KEEP_ATTRIBUTES = ['id','class']
        for tag in soup.findAll(True):
            for key, value in tag.attrs.iteritems():
                if key not in KEEP_ATTRIBUTES:
                    tag.attrs = None
        return soup
    def example():
        doc = editor.getText()
        soup = BeautifulSoup(doc)
        clean_soup = _remove_attrs(soup)
        [comment.extract() for comment in soup.findAll(text=lambda text:isinstance(text, Comment))]
        [item.extract() for item in soup.findAll(text=lambda text:isinstance(text, unicode))]
        editor.setText(clean_soup.prettify().encode('utf8'))
    print example()
    
     
  • fb16
    fb16
    2012-06-03

    Ok, I got it, changing last line from "print example()" into "example()" solves, but, why?… Thanks…

     
  • because example() doesn't return anything. If you put return 'cheese' on the end of the example function, you'll see it adds cheese to the document instead of none. When you say print example(), what your saying is evaluate example() and print the result.

     
  • fb16
    fb16
    2012-06-03

    Hi Dave,

    Ok. My very first steps… :-)

    Thank you

    Regards