我的一些代码:
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
你能告诉我这是什么吗?
答案 0 :(得分:27)
尾随空格是行上最后一个非空白字符后的空格或制表符,直到换行符。
在您发布的问题中,try:
后有一个额外的空格,pass
后有12个额外的空格:
>>> 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 '
查看字符串的结束位置?在行之前有空格(缩进),但也有空格。
使用编辑器查找行尾和退格的结尾。许多现代文本编辑器也可以自动从行尾删除尾随空格,例如每次保存文件时。
答案 1 :(得分:12)
尾随空白:
It is extra spaces (and tabs) at the end of line
^^^^^ here
脱掉它们:
#!/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))
答案 2 :(得分:6)
这只是一个警告,它对您的项目运行没有任何影响,您可以忽略它并继续编码。 但是,如果您像我一样迷恋干净的编码,则有两种选择:
f1
,然后键入trim trailing whitespace
。答案 3 :(得分:2)
我有类似的pep8警告W291 trailing whitespace
long_text = '''Lorem Ipsum is simply dummy text <-remove whitespace
of the printing and typesetting industry.'''
尝试探索尾随空格并删除它们。例如:Lorem Ipsum is simply dummy text