我正在尝试在逐帧加载的网页上自动化流程.我正在尝试设置一个try-except循环,只有在确认元素存在后执行.这是我设置的代码:
from selenium.common.exceptions import NoSuchElementException
while True:
try:
link = driver.find_element_by_xpath(linkAddress)
except NoSuchElementException:
time.sleep(2)
Run Code Online (Sandbox Code Playgroud)
上面的代码不起作用,而以下天真的方法:
time.sleep(2)
link = driver.find_element_by_xpath(linkAddress)
Run Code Online (Sandbox Code Playgroud)
上面的try-except循环中是否有任何遗漏?我尝试了各种组合,包括使用time.sleep()之前try而不是之后except.
谢谢
我被推荐使用第二个,try-except变体,但我也想知道其他人的想法:下面两个(如果有的话)哪个程序更节省时间?
procedure LoadImage(img: TImage; filename: string);
begin
if fileexists(filename) then
img.Picture.Loadfromfile(filename)
else
img.Picture.Loadfromfile('default.jpg')
end;
Run Code Online (Sandbox Code Playgroud)
要么
procedure LoadImage(img: TImage; filename: string);
begin
try
img.Picture.Loadfromfile(filename)
except
img.Picture.Loadfromfile('default.jpg')
end
end;
Run Code Online (Sandbox Code Playgroud) 我一直在努力解决这个错误,现在似乎对于翻译抱怨"继续"的原因有不同的看法.所以我想在下面提供错误的代码.
import tweepy
import time
def writeHandlesToFile():
file = open("dataFile.txt","w")
try:
list = tweepy.Cursor(tweepy.api.followers,screen_name='someHandle',).items(100000)
print "cursor executed"
for item in list:
file.write(item.screen_name+"\n")
except tweepy.error.TweepError as e:
print "In the except method"
print e
time.sleep(3600)
continue
Run Code Online (Sandbox Code Playgroud)
我特别想在最后包括continue的原因是因为我希望程序在睡眠后从它停止的位置重新开始执行,以便保留程序状态.我需要睡眠才能遵守twitter api速率限制,其中api只允许你每小时发出一定数量的请求.所以任何可能看到我的错误天真或其他的人请指出它或请在没有使用continue语句的情况下为我提供替代实现.
顺便说一下,我没有像另一篇文章中所建议的那样混合标签和空格.提前谢谢你的帮助.
我正在尝试将简单的日志放入我的脚本中.此日志应告诉我错误的位置以及修复脚本所需的尽可能多的信息.
我把打印文件str(e)放到每个文件中除外,但它提供了很少的信息来知道出了什么问题.
我怎么能详细说明呢?例如,我可以在控制台中看到整个未捕获的异常文本?
try:
#code
except Exception as e:
print_to_file(log.txt,str(e))
Run Code Online (Sandbox Code Playgroud) 准确性,精确度,召回率和f-score是机器学习系统中系统质量的度量.它取决于真/假阳性/阴性的混淆矩阵.
鉴于二进制分类任务,我尝试了以下方法来获得一个返回准确性,精确度,召回率和f分数的函数:
gold = [1] + [0] * 9
predicted = [1] * 10
def evaluation(gold, predicted):
true_pos = sum(1 for p,g in zip(predicted, gold) if p==1 and g==1)
true_neg = sum(1 for p,g in zip(predicted, gold) if p==0 and g==0)
false_pos = sum(1 for p,g in zip(predicted, gold) if p==1 and g==0)
false_neg = sum(1 for p,g in zip(predicted, gold) if p==0 and g==1)
try:
recall = true_pos / float(true_pos + false_neg)
except:
recall = 0
try:
precision …Run Code Online (Sandbox Code Playgroud) 首先我尝试了这个
def x():
try:
1/0 # just an division error to get an exception
except:
x()
Run Code Online (Sandbox Code Playgroud)
这段代码在 3.10 中表现正常,并且得到了我RecursionError: maximum recursion depth exceeded预期的结果,但 3.8 出现了堆栈溢出,并且无法正确处理递归错误。RecursionError但我确实记得旧版本的Python中也有,所以我尝试了
def x(): x()
Run Code Online (Sandbox Code Playgroud)
RecursionError这在两个版本的 Python 中都有体现。
就好像(在第一个片段中)递归错误永远不会在 except 中抛出,而是在调用的函数中抛出,然后在调用但由 try- except 处理的函数的第一条指令处抛出错误。
然后我尝试了其他方法:
def x():
try:
x()
except:
x()
Run Code Online (Sandbox Code Playgroud)
这在某种程度上更奇怪,3.10 以下的堆栈溢出,但在 3.10 中它陷入了循环
你能解释一下这种行为吗?
更新@MisterMiyagi 发现了一个更奇怪的行为,在 except in 中添加语句<=python3.9不会导致 stackoverflow
def x():
try:
1/0
except:
print("")
x()
Run Code Online (Sandbox Code Playgroud) python stack-overflow recursion try-except pythoninterpreter
假设我有以下例程:
function ReadFile(f : TFilename) : Boolean;
var
fs : TFileStream;
begin
Result := False;
try
fs := TFileStream.Create(f, ...);
try
// read file ...
Result := True;
finally
FreeAndNil(fs);
end;
except
// handle exceptions ...
end;
end;
Run Code Online (Sandbox Code Playgroud)
拥有except和finally转置有什么含义?我已经看到了很多与他们的职位左右逢源左右,但我还没有看到一个明确的解释这是适当的在哪些情况下(我仍然认为这是奇怪的是,在上述构建,该finally块执行后的except块!).
我也看过帖子表明混音try..except和try..finally积木并不是一个好主意.如果例程在正常操作中抛出异常(例如在某些Indy例程中),你怎么能避免它呢?
我正在尝试使用以下代码:
try:
clean = filter(None, re.match(r'^(\S+) (.*?) (\S+)$', full).groups())
except TypeError:
clean = ""
Run Code Online (Sandbox Code Playgroud)
但是我得到了以下追溯......
Traceback (most recent call last):
File "test.py", line 116, in <module>
clean = filter(None, re.match(r'^(\S+) (.*?) (\S+)$', full).groups())
AttributeError: 'NoneType' object has no attribute 'groups'
Run Code Online (Sandbox Code Playgroud)
围绕这个问题的正确异常/正确方法是什么?
当文本文件中的第2行具有"nope"时,它将忽略该行并继续下一行.是否有另一种方法来写这个没有使用尝试,除了?我可以使用if else语句来执行此操作吗?
文本文件示例:
0 1
0 2 nope
1 3
2 5 nope
Run Code Online (Sandbox Code Playgroud)
码:
e = open('e.txt')
alist = []
for line in e:
start = int(line.split()[0])
target = int(line.split()[1])
try:
if line.split()[2] == 'nope':
continue
except IndexError:
alist.append([start, target])
Run Code Online (Sandbox Code Playgroud) 在 python 中,我有处理异常并打印错误代码和消息的代码。
try:
somecode() #raises NameError
except Exception as e:
print('Error! Code: {c}, Message, {m}'.format(c = e.code, m = str(e))
Run Code Online (Sandbox Code Playgroud)
但是,e.code这不是获取错误名称 (NameError) 的正确方法,我找不到答案。我如何获得错误代码?
try-except ×10
python ×8
exception ×3
delphi ×2
continue ×1
file ×1
filter ×1
if-statement ×1
list ×1
nonetype ×1
performance ×1
python-2.7 ×1
recursion ×1
selenium ×1
try-catch ×1
try-finally ×1
twitter ×1