【【转】python try语句相关(try/except/else/finally)】教程文章相关的互联网学习教程文章

python-‘DE’alpha2国家/地区代码的PyCountry货币格式设置出现问题【代码】

我有一个Python函数,该函数接受alpha2国家/地区代码和价格字符串,其目的是获取该国家/地区的货币并使用该货币的currency.letter属性通过字符串插值来格式化提供的价格字符串. 到目前为止,以上方法都可以正常工作-但在以以下国家(德国)的身份调用时,它却落空了:>>> import pycountry >>> country = pycountry.countries.get(alpha2='DE') >>> currency = pycountry.currencies.get(numeric=country.numeric) Traceback (most recen...

python try / SMTPLib除外的问题【代码】

我使用Python的SMTPLib编写了一个简单的SMTP客户端.仅尝试添加一些错误处理-特别是在这种情况下,当要连接的目标服务器不可用时(例如,指定了错误的IP!) 当前,回溯看起来像这样:Traceback (most recent call last):File "<stdin>", line 1, in <module>File "smtplib_client.py", line 91, in runClienttry:File "/usr/local/lib/python2.7/smtplib.py", line 251, in __init__(code, msg) = self.connect(host, port)File "/usr/l...

使用try时是否可能有多个Python除外语句(无需搜索特定的除外)?【代码】

这是我当前的代码:def get_queryset(self)pk = self.kwargs['pk']try: postD = PostD.objects.get(pk=pk)# In the line below, PostReply.objects.filter(postD=postD) is# not guaranteed to exist, so I am using a try statement.return PostReply.objects.filter(postD=postD)except:postY = PostY.objects.get(pk=pk)# In the line below, PostReply.objects.filter(postY=postY) is# not guaranteed to exist either, so th...

python 抓取异常信息try/except【代码】【图】

注意:老版本的Python,except语句写作"except Exception, e",Python 2.6后应写作"except Exception as e"。 格式:try:... except Exception as e:... str(e)返回字符串类型,只给出异常信息,不包括异常信息的类型,如1/0的异常信息‘integer division or modulo by zero’repr(e)给出较全的异常信息,包括异常信息的类型,如1/0的异常信息“ZeroDivisionError(‘integer division or modulo by zero’,)”e.message获得的信息同...

python-ImportError:运行从pyinstaller获得的可执行文件时,没有名为geometry的模块【代码】

Traceback (most recent call last):File "<string>", line 1, in <module> File py_installer/PyInstaller-2.1/PyInstaller/loader/pyi_importers.py", line 270, in load_module File py_installer/PyInstaller-2.1/FaceMatcher/build/FaceMatcher/out00-PYZ.pyz/proj_code", line 11, in <module> File PyInstaller-2.1/PyInstaller/loader/pyi_importers.py", line 270, in load_module File PyInstaller-2.1/FaceMatcher/buil...

python-如何使用PY_VAR0或PY_VAR1获得Entry值?【代码】

我有这样的简单代码:from tkinter import *def _show_value(*pargs):print(*pargs)root = Tk()entry_var1 = StringVar() entry_var1.trace('w', _show_value)entry_var2 = StringVar() entry_var2.trace('w', _show_value)e1 = Entry(root, textvariable=entry_var1) e1.pack()e2 = Entry(root, textvariable=entry_var2) e2.pack() root.mainloop()我想在每次写入时动态打印e1和e2的内容.但是我得到的不是文字,而是:PY_VAR...

python-填充字典而不使用try除外【代码】

假设我有字典,我想用一些键和值填充它,第一个字典是空的,并假设我需要这个字典作为计数器,例如用这种方式对字符串中的一些键进行计数:myDic = {} try :myDic[desiredKey] += 1 except KeyError:myDic[desiredKey] = 1也许有时候值应该是列表,而我需要以这种方式将一些值附加到值列表中:myDic = {} try:myDic[desiredKey].append(desiredValue) except KeyError:myDic[desiredKey] = []myDic[desiredKey].append(desiredValue)对于...

Python try块不会捕获os.system异常【代码】

我有这个python代码:import os try:os.system('wrongcommand') except:print("command does not work")代码打印:wrongcommand: command not found而不是命令不起作用.有谁知道为什么它不打印我的错误信息?解决方法:如果要在命令不存在时抛出异常,则应使用子进程:import subprocesstry:subprocess.call(['wrongcommand'])except OSError:print ('wrongcommand does not exist')想想看,你应该使用subprocess而不是os.system …

Python Tkinter 文本框(Entry)【图】

Python Tkinter 文本框用来让用户输入一行文本字符串。你如果需要输入多行文本,可以使用 Text 组件。你如果需要显示一行或多行文本且不允许用户修改,你可以使用 Label 组件。语法 语法格式如下:w = Entry( master, option, ... )master: 按钮的父容器。options: 可选项,即该按钮的可设置的属性。这些选项可以用键 = 值的形式设置,并以逗号分隔。 方法 下表为文本框组件常用的方法: 实例 实例中点击按钮会显示一个信息...

python – Tkinter Entry未显示textvariable的当前值【代码】

考虑以下代码:from tkinter import * from tkinter.ttk import *tk=Tk()def sub():var=StringVar(value='default value')def f(): passEntry(tk,textvariable=var).pack()Button(tk,text='OK',command=f).pack()sub() mainloop()我们期望var的值出现在条目中,但实际上并非如此.奇怪的是,如果我将语句var.get()放在按钮的回调函数中,则var的值将为apear.这是由Python中的某种局部变量优化引起的错误吗?我该怎么做才能确保textvari...

python – 使用scrapy中的try / except子句无法获得所需的结果【代码】

我在scrapy中编写了一个脚本,通过get_proxies()方法使用新生成的代理来生成代理请求.我使用请求模块来获取代理,以便在脚本中重用它们.我正在尝试解析它的所有电影链接是landing page,然后从它的target page获取每部电影的名称.我的下面的脚本可以使用代理的旋转. 我知道有一种更简单的方法来更改代理,就像这里描述的那样HttpProxyMiddleware但是我仍然想坚持我在这里尝试的方式. website link 这是我当前的尝试(它不断使用新代理来...

python sys.exit无法在try 中工作【代码】

参见英文答案 > Why is “except: pass” a bad programming practice? 16个 Python 2.7.5 (default, Feb 26 2014, 13:43:17) [GCC 4.4.7 20120313 (Red Hat 4.4.7-4)] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>> import sys >>> try: ... sys.exit() ... except: ... print "in except" ... in except >>> try: ... sys.exit(0) ... except: ...

python – 与pyspark中的scala.util.Try相同的是什么?【代码】

我有一个糟糕的HTTPD access_log,只想跳过“糟糕”的行. 在scala中,这很简单:import scala.util.Tryval log = sc.textFile("access_log")log.map(_.split(' ')).map(a => Try(a(8))).filter(_.isSuccess).map(_.get).map(code => (code,1)).reduceByKey(_ + _).collect()对于python我通过使用“lambda”表示法明确定义函数来获得以下解决方案:log = sc.textFile("access_log")def wrapException(a):try:return a[8]except:return...

python – MaxRetryError:HTTPConnectionPool:超出最大重试次数(由ProtocolError引起(‘连接中止.’,错误(111,’拒绝连接’)))【代码】

我有一个问题:我想测试“选择”和“输入”.我可以像下面的代码一样编写它:原始代码:12 class Sinaselecttest(unittest.TestCase):13 14 def setUp(self):15 binary = FirefoxBinary('/usr/local/firefox/firefox')16 self.driver = webdriver.Firefox(firefox_binary=binary)17 18 def test_select_in_sina(self):19 driver = self.driver20 driver.get("https://www.sina.com.cn/")21...

TRY - 相关标签