用python cod合并try-except块

2024-06-17 12:10:02 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在用返回一个键和一个对应值的多个查询填充一个电子表格。脚本中使用了两个数据库连接来返回数据。你知道吗

如果正在执行的SQL语句没有问题,那么下面的代码可以很好地工作。如果SQL有问题,程序会出错退出,并且我的电子表格不会被填充。你知道吗

如果有错误,我仍希望填充第一列中的键,并使值返回“0”

棘手的部分是,因为有两个服务器连接,一个服务器可以很好地处理SQL,另一个服务器可能抛出异常。你知道吗

有没有一种方法可以转换这个代码块来处理这个问题?你知道吗

我是个Python新手,所以深入研究错误处理有点让人望而生畏

# Populate the spreadsheet with data from the first set of date ranges.
row = 1
col = 0

for key, value in Queries.query_dic.iteritems():
    cur.execute(value.format(from_dateA,to_dateA))
    cur2.execute(value.format(from_dateA,to_dateA))
    rows = cur.fetchall()
    rows2 = cur2.fetchall()

    # Populate metric being queried in our horizontal headers
    worksheet[index].write(row, col, key, format)
    worksheet[index + 1].write(row, col, key, format)

    # Iterate over the data and write it out row by row.
    for return_count in rows:
        worksheet[index].write(row, col + 1, return_count[0], format2)

    for return_count in rows2:
        worksheet[index + 1].write(row, col + 1, return_count[0], format2)

    row += 1

Tags: theinfrom服务器formatforsqlindex
2条回答

经过反复试验,我找到了一个可行的解决办法。你知道吗

# Print metric headers and counts for first date range.
row = 1
col = 0

for key, value in Queries.query_dic.iteritems():

    # Write the metrics headers
    worksheet[index].write(row, col, key, format)
    worksheet[index + 1].write(row, col, key, format)

    # Populate spreadsheet with count returned from query for Environment 1. 
    try:
        cur.execute(value.format(from_dateA,to_dateA))
        rows = cur.fetchall()
        for return_count in rows:
            worksheet[index].write(row, col + 1, return_count[0], format2)
    # Query will fail if module doesn't exist; in this case print '0'.
    except:
        worksheet[index].write(row, col + 1, '0', format2)

    # Populate spreadsheet with count returned from query for Environment 2. 
    try:
        cur2.execute(value.format(from_dateA,to_dateA))
        rows = cur2.fetchall()
        for return_count in rows:
            worksheet[index + 1].write(row, col + 1, return_count[0], format2)
    # Query will fail if module doesn't exist; in this case print '0'.
    except:
        worksheet[index + 1].write(row, col + 1, '0', format2)

    row += 1

除非你能做到这一点

try:
    for key, value in Queries.query_dic.iteritems():
        cur.execute(value.format(from_dateA,to_dateA))
        cur2.execute(value.format(from_dateA,to_dateA))
        rows = cur.fetchall()
        rows2 = cur2.fetchall()

        # Populate metric being queried in our horizontal headers
        worksheet[index].write(row, col, key, format)
        worksheet[index + 1].write(row, col, key, format)

        # Iterate over the data and write it out row by row.
        for return_count in rows:
            worksheet[index].write(row, col + 1, return_count[0], format2)

        for return_count in rows2:
            worksheet[index + 1].write(row, col + 1, return_count[0], format2)

        row += 1
except:
    # Error Handling

如果您想进行特定于错误的处理,只需将错误放在

except KeyboardInterrupt:
    # Error Handling

相关问题 更多 >