Pythonic方式写了很多try / except子句?

时间:2016-09-12 13:09:58

标签: python excel

我正在编写一个脚本,用于将200个有些独特数据的单元格写入excel电子表格。我的代码遵循这个基本模式:

try:
    sheet.cell(row=9, column=7).value = getData()
except:
    sheet.cell(row=9, column=7).value = 'NA'

基本上如果有错误,请将一些占位符写入单元格。我想知道是否有一种更短,更pythonic的方式来写这个,而不是依次有200个。

2 个答案:

答案 0 :(得分:7)

据我所知,getData()调用可能会发生异常。所以你可以将getData函数包装为:

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions"%>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form"%>
<%@ taglib prefix="spring" uri="http://www.springframework.org/tags"%>
<%@ taglib prefix="sec" uri="http://www.springframework.org/security/tags" %>
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Login</title>
</head>
<body>
    <form:form role="registerModel" commandName="registerModel" method="POST" action="${pageContext.request.contextPath}/zone4/simple/register.htm">
        <form:errors path="*" cssClass="errorblock" element="div" />
        <fieldset>
            <div class="form-group">
                <form:input path="mail1" id="mail1" name="mail1" placeHolder="email" />
                <form:errors path="mail1" id="mail1" name="mail1" placeHolder="email" cssClass="error" />
            </div>
            <div class="form-group">
                <form:input path="mail2" id="mail2" name="mail2" placeHolder="email" />
                <form:errors path="mail2" id="mail2" name="mail2" placeHolder="email" cssClass="error" />
            </div>
            <div class="form-group">
                <form:input path="password1" id="password1" name="password1" placeHolder="password" />
                <form:errors path="password1" id="password1" name="password1" placeHolder="password" cssClass="error" />
            </div>
            <div class="form-group">
                <form:input path="password2" id="password2" name="password2" placeHolder="password" />
                <form:errors path="password2" id="password2" name="password2" placeHolder="password" cssClass="error" />                
            </div>
            <div class="checkbox">
                <label>
                    <input name="remember-me" id="remember-me" type="checkbox" value="Remember Me">Remember Me
                </label>
            </div>
            <!-- Change this to a button or input when using this as a form -->
            <button type="submit" class="btn btn-lg btn-success btn-block" name="submit"><spring:message code="lblRegistration"/></button>
        </fieldset>
    </form:form>
</body>
</html>

然后使用:

def get_data_or_default(default="NA"):
   try:
       return getData()
   except Exception:
       return default

答案 1 :(得分:0)

将所有单元格放入列表中,或者更好,从嵌套的for i in xrange()循环中获取列/行,以便您可以编写一行代码来执行所有单元格。

for r in xrange(0, 10):
    for c in xrange(0, 10):
        try:
            sheet(row=r, column=c).value = getData()
        except BadDataException:
            sheet(row=r, column=c).value = 'NA'

但正如其他人所提到的,如果你可以改变getData,让它返回默认值(或None)而不是引发异常。如果您无法更改getData,请查看文档以查看它是否具有某种default选项,例如许多Python对象(例如,带有.get()的词典)

UPDATE :或者,如果您无法自行更改,请参阅Zakhar对包装getData的回答。