草庐IT

try-exception

全部标签

Python: "except KeyError"比 "if key in dict"快吗?

编辑2:有人建议这是一个类似问题的副本。我不同意,因为我的问题集中在速度上,而另一个问题询问什么更“可读”或“更好”(没有定义更好)。虽然问题相似,但给出的讨论/答案却有很大差异。EDIT:IrealisefromthequestionsthatIcouldhavebeenclearer.Sorryforcodetypos,yesitshouldbeusingtheproperpythonoperatorforaddition.Regardingtheinputdata,Ijustchosealistofrandomnumberssincethat'sacommonsample.Inm

python - 使用裸 'except' 有什么问题?

这个问题在这里已经有了答案:HowcanIwritea`try`/`except`blockthatcatchesallexceptions?(10个答案)关闭3年前。社区在10个月前审查了是否重新打开此问题,然后将其关闭:原始关闭原因未解决我尝试使用PyAutoGui创建一个函数来检查图像是否显示在屏幕上,并想出了这个:defcheck_image_on_screen(image):try:pyautogui.locateCenterOnScreen(image)returnTrueexcept:returnFalse它工作正常,但PyCharm告诉我我不应该让except空着。就这

python - 尽管安装了 GEOS,但获取 "django.core.exceptions.ImproperlyConfigured: GEOS is required and has not been detected."

我在Ubuntu14.04LTS上运行Django1.8和Python3.4。就在最近,我的Django应用一直在报告GEOS不存在。GEOS已安装并且libgeos_c.so位于它应该位于的位置(/usr/lib/)。我的代码看起来不错。它是仍然有效的docker镜像的来源。这似乎表明操作系统/不兼容问题。任何帮助将不胜感激。完整的追溯是Traceback(mostrecentcalllast):File"/pycharm-4.5.1/helpers/pydev/pydevd.py",line2358,inglobals=debugger.run(setup['file'],None

python - 加密 : AssertionError ("PID check failed. RNG must be re-initialized after fork(). Hint: Try Random.atfork()")

我正在创建执行不同任务的各种流程。其中之一,也是唯一一个,有一个创建PyCrypto对象的安全模块。所以我的程序启动,创建各种进程,处理消息的进程使用安全模块解密,我得到以下错误:firstSymKeybin=self.cipher.decrypt(encFirstSymKeybin,'')File"/usr/local/lib/python2.7/dist-packages/Crypto/Cipher/PKCS1_v1_5.py",line206,indecryptm=self._key.decrypt(ct)File"/usr/local/lib/python2.7/dist-pa

python - 为什么 "except Exception"没有捕捉到 SystemExit?

isinstance(SystemExit(1,),Exception)评估为True,但此代码段打印“caughtbybareexceptSystemExit(1,)”。try:sys.exit(0)exceptException,e:print'caughtbyexceptException',str(e)except:print'caughtbybareexcept',repr(sys.exc_info()[1])我的测试环境是Python2.6。 最佳答案 isinstance(SystemExit(1),异常)在Pytho

python - 错误 : No Commands supplied when trying to install pyglet

我已经下载了pyglet,但是当我运行“setup.py”,它只是在命令行:Traceback(mostrecentcalllast):File"C:\PythonX\Include\pyglet\pyglet-1.1.4\setup.py",line285,insetup(**setup_info)File"C:\Python27\lib\distutils\core.py",line140,insetupraiseSystemExit,gen_usage(dist.script_name)+"\nerror:%s"%msgSystemExit:usage:setup.py[glob

python - Python中如何正确使用try,except,else

所以我想知道编写tryexcept语句的正确方法是什么。我是Python错误处理的新手。选项1try:itemCode=items["itemCode"]dbObject=db.GqlQuery("SELECT*FROM%sWHEREcode=:1"%dbName,itemCode).get()dbObject.delete()exceptAttributeError:print"There'snoitemwiththatcode"exceptKeyError:print"Badparametername"except:print"Unknowerror"选项2try:itemCode

python - 无一异常(exception)地获取 python 回溯

假设您有这些模块:模块1.pyimportmodule2defa():module1.b()defc():print"Higuys!"模块2.pyimportmodule1defb():module1.c()我想要一个函数func(a())产生与此类似的输出:(=atraceback?)/usr/local/lib/python2.7/dist-packages/test/module1.py3defa():4module1.b()1importmodule1/usr/local/lib/python2.7/dist-packages/test/module2.py3defb():4m

python - 为什么 `if Exception` 在 Python 中工作

在这个答案https://stackoverflow.com/a/27680814/3456281中,提出了以下结构a=[1,2]whileTrue:ifIndexError:print("Stopped.")breakprint(a[2])实际上打印“已停止”。和中断(使用Python3.4.1测试)。为什么?!为什么ifIndexError甚至是合法的?为什么a[2]不会在没有try...except的情况下引发IndexError? 最佳答案 所有对象都有一个boolean值。如果没有另外定义,则该boolean值为True。

python - 如何在 python 中的一个 block 中编写多个 try 语句?

我想做的事:try:do()except:do2()except:do3()except:do4()如果do()失败,则执行do2(),如果do2()也失败,则执行do3()等。最好的问候 最佳答案 如果你真的不关心异常,你可以遍历案例直到你成功:forfnin(do,do2,do3,do4):try:fn()breakexcept:continue这至少避免了每次都缩进一次。如果不同的函数需要不同的参数,您可以使用functools.partial在循环之前“初始化”它们。 关于pyt