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?

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.

Why is trailing whitespace a big deal?, Trailing whitespace is any spaces or tabs after the last non-whitespace See this article on what Emacs offers to handle trailing whitespaces:� The trimws function trims leading and/or trailing whitespace from a character string. The following article explains in an actionable examples how to trim leading and trailing whitespace with the trimws command in R. Let’s dive in! Step 1: Create Example Data. Before we can apply the trimws function, we need to create some example data:

Trailing whitespace:

It is extra spaces (and tabs) at the end of line      
                                                 ^^^^^ here

Strip them:

#!/usr/bin/env python2
"""\
strip trailing whitespace from file
usage: stripspace.py <file>
"""

import sys

if len(sys.argv[1:]) != 1:
  sys.exit(__doc__)

content = ''
outsize = 0
inp = outp = sys.argv[1]
with open(inp, 'rb') as infile:
  content = infile.read()
with open(outp, 'wb') as output:
  for line in content.splitlines():
    newline = line.rstrip(" \t")
    outsize += len(newline) + 1
    output.write(newline + '\n')

print("Done. Stripped %s bytes." % (len(content)-outsize))

https://gist.github.com/techtonik/c86f0ea6a86ed3f38893

Trailing whitespaces: what you gonna do about it? – Sinaru, Googling the phrase "trailing whitespace" is like googling "coffee Just to be clear, by "whitespace" I mean the ASCII blank space: hex 20, Other kinds of whitespace, like non-breaking spaces, I detect and treat as gremlin� How XML Processors Handle XML Whitespace. XML standards specify how XML processors should handle the whitespace. XML Parsing: The XML spec provides a built-in attribute xml:space to tell the XML parser whether it should ignore the whitespace characters. This attribute is inherited by child elements from their root element.

This is just a warning and it doesn't make problem for your project to run, you can just ignore it and continue coding. But if you're obsessed about clean coding, same as me, you have two options:

  1. Hover the mouse on warning in VS Code or any IDE and use quick fix to remove white spaces.
  2. Press f1 then type trim trailing whitespace.

Leading and trailing whitespace, We can use the method rstrip() which returns a copy of the string in which all specified chars have been stripped from the end of the string. While not strictly programming, whitespace can seriously mess up data entry, if there's trailing/leading in a file that will be parsed and used as input to something else. This happens the most when a clean, generated input file gets dirtied by someone editing it in Excel or something and then trailing whitespace (and other formatting issueS) can creep in.

I have got similar pep8 warning W291 trailing whitespace

long_text = '''Lorem Ipsum is simply dummy text  <-remove whitespace
of the printing and typesetting industry.'''

Try to explore trailing whitespaces and remove them. ex: two whitespaces at the end of Lorem Ipsum is simply dummy text

How to remove all trailing whitespace of string in Python?, Let's have a look at different functions to remove spaces. strip(). Python String strip() function will remove leading and trailing whitespaces. >>� Probably the best way is to handle the trailing white spaces when you read your data file. If you use read.csv or read.table you can set the parameterstrip.white=TRUE.. If you want to clean strings afterwards you could use one of these functions:

Python Remove Spaces from String, This extension will highlight and remove any trailing whitespace on any line in any editor in Visual Studio. This makes it really easy to get rid of� 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. Let’s say the following is our string with leading and trailing spaces − String str = new String(" Jack Sparrow ");

Trailing Whitespace Visualizer, disallow trailing whitespace at the end of lines (no-trailing-spaces). The --fix option on the command line can automatically fix some of the problems reported by� Those settings (core.whitespace and apply.whitespace) are not there to remove trailing whitespace but to: core.whitespace: detect them, and raise errors ; apply.whitespace: and strip them, but only during patch, not "always automatically" I believe the git hook pre-commit would do a better job for that (includes removing trailing whitespace)

no-trailing-spaces - Rules - ESLint, You can make trailing whitespace at the end of a line visible by setting the buffer- local variable show-trailing-whitespace to t . Then Emacs displays trailing� You can use the following formula. =TRIM(B1) Type this formula into a blank cell and then press Enter key, and drag the AutoFill Handle over other cells to apply this formula. This formula will remove both leading and trailing spaces from text string in cells. Remove Trailing Spaces from Cells with VBA Macro

Comments
  • In emacs: C-M-% <space> + $ then press return twice. Wow, that looks more cryptic than it feels when I type it.
  • surely trailing whitespace is valid (and reported by PEP8) on every line, not only after :, right?
  • @Christoph: sorry, yes, all trailing whitespace; that was an editing error.