存储 Python PEP8 模块输出
Store Python PEP8 module output
我在代码中使用 python 的 PEP8 模块。
import pep8
pep8_checker = pep8.StyleGuide(format='pylint')
pep8_checker.check_files(paths=['./test.py'])
r = pep8_checker.check_files(paths=['./test.py'])
这是输出:
./test.py:6: [E265] block comment should start with '# '
./test.py:23: [E265] block comment should start with '# '
./test.py:24: [E302] expected 2 blank lines, found 1
./test.py:30: [W293] blank line contains whitespace
./test.py:35: [E501] line too long (116 > 79 characters)
./test.py:41: [E302] expected 2 blank lines, found 1
./test.py:53: [E501] line too long (111 > 79 characters)
./test.py:54: [E501] line too long (129 > 79 characters)
但是这个结果打印在终端上,分配给 'r' 的最终值是 8(即错误总数)。
我想将这些错误存储在一个变量中。我该怎么做?
编辑:
这是 test.py 文件:http://paste.fedoraproject.org/347406/59337502/raw/
至少有两种方法可以做到这一点。最简单的方法是将 sys.stdout
重定向到一个文本文件,然后随意阅读该文件:
import pep8
import sys
saved_stdout = sys.stdout
sys.stdout = open('pep8.out', 'w')
pep8_checker = pep8.StyleGuide(format='pylint')
pep8_checker.check_files(paths=['./test.py'])
r = pep8_checker.check_files(paths=['./test.py'])
sys.stdout.close()
sys.stdout = saved_stdout
# Now you can read "pep.out" into a variable
或者,您可以使用 StringIO
:
写入变量
import pep8
import sys
# The module name changed between python 2 and 3
if sys.version_info.major == 2:
from StringIO import StringIO
else:
from io import StringIO
saved_stdout = sys.stdout
sys.stdout = StringIO()
pep8_checker = pep8.StyleGuide(format='pylint')
pep8_checker.check_files(paths=['./test.py'])
r = pep8_checker.check_files(paths=['./test.py'])
testout = sys.stdout.getvalue()
sys.stdout.close()
sys.stdout = saved_stdout
# testout contains the output. You might wish to testout.spilt("\n")
我在代码中使用 python 的 PEP8 模块。
import pep8
pep8_checker = pep8.StyleGuide(format='pylint')
pep8_checker.check_files(paths=['./test.py'])
r = pep8_checker.check_files(paths=['./test.py'])
这是输出:
./test.py:6: [E265] block comment should start with '# '
./test.py:23: [E265] block comment should start with '# '
./test.py:24: [E302] expected 2 blank lines, found 1
./test.py:30: [W293] blank line contains whitespace
./test.py:35: [E501] line too long (116 > 79 characters)
./test.py:41: [E302] expected 2 blank lines, found 1
./test.py:53: [E501] line too long (111 > 79 characters)
./test.py:54: [E501] line too long (129 > 79 characters)
但是这个结果打印在终端上,分配给 'r' 的最终值是 8(即错误总数)。
我想将这些错误存储在一个变量中。我该怎么做?
编辑: 这是 test.py 文件:http://paste.fedoraproject.org/347406/59337502/raw/
至少有两种方法可以做到这一点。最简单的方法是将 sys.stdout
重定向到一个文本文件,然后随意阅读该文件:
import pep8
import sys
saved_stdout = sys.stdout
sys.stdout = open('pep8.out', 'w')
pep8_checker = pep8.StyleGuide(format='pylint')
pep8_checker.check_files(paths=['./test.py'])
r = pep8_checker.check_files(paths=['./test.py'])
sys.stdout.close()
sys.stdout = saved_stdout
# Now you can read "pep.out" into a variable
或者,您可以使用 StringIO
:
import pep8
import sys
# The module name changed between python 2 and 3
if sys.version_info.major == 2:
from StringIO import StringIO
else:
from io import StringIO
saved_stdout = sys.stdout
sys.stdout = StringIO()
pep8_checker = pep8.StyleGuide(format='pylint')
pep8_checker.check_files(paths=['./test.py'])
r = pep8_checker.check_files(paths=['./test.py'])
testout = sys.stdout.getvalue()
sys.stdout.close()
sys.stdout = saved_stdout
# testout contains the output. You might wish to testout.spilt("\n")