在python中检查错误

时间:2011-03-04 18:23:34

标签: python error-handling

在与脾气暴躁的CSV字段进行数据下载之后挣扎。如何使用try / Except格式。

LL = [(XXX,YYY,ZZZ),] or [[XXX,YYY,ZZZ],] 

如果在上面,我如何在下面做?

try: 
   IF XXX or YYY or ZZZ or AAA == 'N/A',
   (dont process data...skip to except and pass)
except:
   pass 

站在这里: Remove/Replace Error from Tuple in python

3 个答案:

答案 0 :(得分:1)

已更新

我想是那样的

try: 
   if "N/A" in [XXX,YYY,ZZZ,AAA]
       raise Exception()
except:
   pass 

答案 1 :(得分:1)

for data in LL:
   try:
      if "N/A" in data:
         continue
      else:
         x, y, z = data
         # Process data...
   except Exception:
      continue

答案 2 :(得分:1)

请注意,执行普通except:通常是一个坏主意,因为它会吞下您需要了解的异常。

LL = [("bad line",456,"N/A"),["good line", 123, 456],]

for line in LL:
    try: 
        if "N/A" in line:
            raise ValueError

        print line[0]

    except ValueError:
        print "skipped"