简体   繁体   English

Python脚本利用pass进行异常处理,pass似乎没有正常运行

[英]Python script utilizing pass for exception handling, pass doesn't seem to function properly

I am working on a script that is going to back-fill Call Detail Records from our asterisk system into our MySQL logs database. 我正在编写一个脚本,它将把我们的星号系统中的Call Detail Records重新填充到我们的MySQL日志数据库中。 In the code below, I am trying to ignore the duplicate keys and continue onto the next row, however when this code executes all I ever see is the first row dupe warning then the script exits (example 2, below.) Is there a an obvious error that's causing it to fail? 在下面的代码中,我试图忽略重复的键并继续到下一行,但是当这段代码执行时,我看到的是第一行欺骗警告,然后脚本退出(下面的示例2)。是否有一个明显的错误导致它失败?

Forgive my poor python etiquette, I'm very new to the language. 原谅我糟糕的蟒蛇礼仪,我对语言很新。 My assumption is that even though pass might break out of an exception, the foreach loop doesn't survive. 我的假设是即使pass可能会突破异常,foreach循环也无法生存。

EDIT/NOTE : This is also worth mentioning after I solved the problem of the exiting for loop: because I had a finally: block which closed the sql connection, the finally: block was being executed after the except: pass and closing the connection. 编辑/注意 :在我解决了退出for循环的问题后,这也值得一提:因为我有一个finally:块关闭了sql连接, finally:块正在执行except: pass和关闭连接。 So in the above example, the finally: was still causing the program to abend. 所以在上面的例子中, finally:仍然导致程序异常终止。

#!/usr/bin/python -d

import csv
import sys
import MySQLdb as mdb
log="Master.csv"

try:
        con = mdb.connect('1.2.3.4','abcd','efgh','ijkl')
        cur = con.cursor()

        #Inefficient way of getting row count.
        rcount = csv.reader(open(log, 'rb'))
        print "Number of rows in csv: %d" % (len(list(rcount)))

        #OK, real csv processing now.
        reader = csv.reader(open(log, 'rb'))

        iter = 0
        for row in reader:
                print "Row: %d" % (++iter)

                clid = row[0]
                src = row[1]
                dst = row[2]
                dcontext = row[3]
                channel = row[4]
                dstchannel = row[5]
                lastapp = row[6]
                lastdata = row[7]
                start = row[8]
                end = row[10]
                duration = row[11]
                billsec = row[12]
                disposition = row[13]
                amaflags = row[14]
                accountcode = row[15]
                uniqueid = row[16]

                insertstr= "INSERT INTO cdr_extended (duration,billsec,amaflags,start,end,clid,src,dst,dcontext,channel,dstchannel,lastapp,lastdata,disposition,accountcode,uniqueid) VALUES (%s,%s,0,'%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s');" % (duration,billsec,start,end,clid,src,dst,dcontext,channel,dstchannel,lastapp,lastdata,disposition,accountcode,uniqueid)
                cur.execute(insertstr)
                con.commit()

except mdb.Error, e:
        if e.args[0] == 1062:
                print "Dupe key on uniqueid: %s"  % (uniqueid)
                pass
        else:
                print "Error %d: %s" % (e.args[0],e.args[1])
                sys.exit(1)

finally:
        if con:
                con.close()

The output: 输出:

Number of rows in csv: 2696
Row: 0
Dupe key on uniqueid: 1342632723.8

Wrap your try...except block only around the code that would cause the exception, which is in the for loop. 包装你的try...except阻止只会导致异常的代码,这是在for循环中。 I don't use MySQLdb , but something like this should work: 我不使用MySQLdb ,但这样的东西应该工作:

            try:
                insertstr= "INSERT INTO cdr_extended (duration,billsec,amaflags,start,end,clid,src,dst,dcontext,channel,dstchannel,lastapp,lastdata,disposition,accountcode,uniqueid) VALUES (%s,%s,0,'%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s');" % (duration,billsec,start,end,clid,src,dst,dcontext,channel,dstchannel,lastapp,lastdata,disposition,accountcode,uniqueid)
                cur.execute(insertstr)
                con.commit()
            except mdb.Error, e:
                if e.args[0] == 1062:
                    print "Dupe key on uniqueid: %s"  % (uniqueid)
                else:
                    print "Error %d: %s" % (e.args[0],e.args[1])
                    sys.exit(1)

pass is used in python in place of empty curly braces of other programming languages. pass在python中用来代替其他编程语言的空花括号。

if x>0: pass

is equal to c 's: if (x>0); 等于c的: if (x>0); or if (x>0){} 或者if (x>0){}

use continue to continue the for loop. 使用continue继续for循环。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM