Python clean way to wrap individual statements in a try except block

前端 未结 4 1446
情话喂你
情话喂你 2021-01-17 21:14

I\'m currently doing some Python automation of Excel with com. It\'s fully functional, and does what I want, but I\'ve discovered something surprising. Sometimes, some of th

4条回答
  •  旧时难觅i
    2021-01-17 21:47

    You can zip arguments from three list, and do the following:

    for border, attr, value in myArgs:
        while True:
            i = 0
            try:
                setattr(excel.Selection.Borders(border), attr, value) 
            except:
                if i>100:
                    break
            else:
                break
    

    If your exceptions are trully random, this will try until success (with a limit of 100 tries). I don't recommend this.

提交回复
热议问题