Python 什么是尾随空格,我该如何处理?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21410075/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
what is trailing whitespace and how can I handle this?
提问by Amy Obrian
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:
pep8 警告是:
trailing whitespace 37:try
trailing whitespace 43:pass
Can you please tell me what is this?
你能告诉我这是什么吗?
回答by Martijn Pieters
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:
在您发布的问题中, 之后有一个额外的空格,之后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 '
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.
使用您的编辑器查找行尾和退格。许多现代文本编辑器还可以自动删除行尾的尾随空格,例如每次保存文件时。
回答by anatoly techtonik
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))
回答by heykarimoff
I have got similar pep8 warning W291 trailing whitespace
我有类似的 pep8 警告 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
尝试探索尾随空格并将其删除。例如:末尾的两个空格Lorem Ipsum is simply dummy text
回答by Mohammad Hashemi
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:
这只是一个警告,它不会给您的项目运行带来问题,您可以忽略它并继续编码。但是如果你和我一样痴迷于干净的编码,你有两个选择:
- Hover the mouse on warning in VS Code or any IDE and use quick fix to remove white spaces.
- Press
f1then typetrim trailing whitespace.
- 将鼠标悬停在 VS Code 或任何 IDE 中的警告上,并使用快速修复来删除空格。
- 按
f1然后键入trim trailing whitespace。

