73

私のコードの一部:

            if self.tagname and self.tagname2 in list1:
                try: 
                    question = soup.find("div", "post-text")
                    title = soup.find("a", "question-hyperlink")
                    self.list2.append(str(title)+str(question)+url)
                    current += 1
                except AttributeError:
                    pass            
            logging.info("%s questions passed, %s questions \
                collected" % (count, current))
            count += 1
        return self.list2

pep8 警告は次のとおりです。

trailing whitespace 37:try
trailing whitespace 43:pass

これが何なのか教えてください。

4

4 に答える 4

48

末尾の空白は、行の最後の非空白文字から改行までの空白またはタブです。

投稿された質問では、 の後に 1 つの余分なスペースがtry:あり、 の後に 12 の余分なスペースがありますpass

>>> post_text = '''\
...             if self.tagname and self.tagname2 in list1:
...                 try: 
...                     question = soup.find("div", "post-text")
...                     title = soup.find("a", "question-hyperlink")
...                     self.list2.append(str(title)+str(question)+url)
...                     current += 1
...                 except AttributeError:
...                     pass            
...             logging.info("%s questions passed, %s questions \
...                 collected" % (count, current))
...             count += 1
...         return self.list2
... '''
>>> for line in post_text.splitlines():
...     if line.rstrip() != line:
...         print(repr(line))
... 
'                try: '
'                    pass            '

文字列がどこで終わるか分かりますか? 行 (インデント) の前にスペースがありますが、後にもスペースがあります。

エディターを使用して、行末とバックスペースを見つけます。最新のテキスト エディターの多くは、たとえばファイルを保存するたびに、行末から末尾の空白を自動的に削除することもできます。

于 2014-01-28T15:40:49.440 に答える