Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

what is trailing whitespace and how can I handle this?

some piece of my codes:

            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 warning is:

trailing whitespace 37:try trailing whitespace 43:pass 

Can you please tell me what is this?

like image 365
Amy Obrian Avatar asked Jan 28 '14 15:01

Amy Obrian


People also ask

How do you get rid of trailing white space?

Type M-x delete-trailing-whitespace to delete all trailing whitespace. This command deletes all extra spaces at the end of each line in the buffer, and all empty lines at the end of the buffer; to ignore the latter, change the variable delete-trailing-lines to nil .

How do I fix trailing spaces in Python?

Python String strip() function will remove leading and trailing whitespaces. If you want to remove only leading or trailing spaces, use lstrip() or rstrip() function instead.

How do you get rid of leading and trailing white spaces?

To remove leading and trailing spaces in Java, use the trim() method. This method returns a copy of this string with leading and trailing white space removed, or this string if it has no leading or trailing white space.


1 Answers

Trailing whitespace is any spaces or tabs after the last non-whitespace character on the line until the newline.

In your posted question, there is one extra space after try:, and there are 12 extra spaces after 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            ' 

See where the strings end? There are spaces before the lines (indentation), but also spaces after.

Use your editor to find the end of the line and backspace. Many modern text editors can also automatically remove trailing whitespace from the end of the line, for example every time you save a file.

like image 116
Martijn Pieters Avatar answered Sep 22 '22 12:09

Martijn Pieters