让我们说我有一个字符串
s="""
print 'hi'
print 'hi'
print 3333/0
"""
Run Code Online (Sandbox Code Playgroud)
是否有可以帮助我检查此字符串语法的模块或方法?
我希望输出如下:
第2行,缩进第3行,除以零
我听说过pyFlakes,pyChecker和pyLint,但那些检查文件,而不是字符串.
compile()函数将告诉您编译时错误:
try:
compile(s, "bogusfile.py", "exec")
except Exception as e:
print "Problem: %s" % e
Run Code Online (Sandbox Code Playgroud)
请记住:一个错误会阻止报告其他错误,并且您的一些错误(ZeroDivision)是运行时错误,而不是编译器检测到的错误.