Merlin
Merlin

Reputation: 25639

Error checking in python

After struggling with data download with ill tempered CSV fields. How could use try/Except format.

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

if above, how do i do below?

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

staeted here: Remove/Replace Error from Tuple in python

Upvotes: 1

Views: 131

Answers (3)

nesv
nesv

Reputation: 812

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

Upvotes: 1

Xavier Combelle
Xavier Combelle

Reputation: 11195

UPDATED

I suppose like that

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

Upvotes: 1

Martin Stone
Martin Stone

Reputation: 13007

Note that it's generally a bad idea to do a plain except:, as it will swallow exceptions that you need to know about.

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"

Upvotes: 1

Related Questions