1

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

3 Answers 3

1

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"
Sign up to request clarification or add additional context in comments.

4 Comments

-1 Raising an exception only to trap it yourself in the same try block is ludicrous. Use if/else!
@John Machin, OP asked for an example using exceptions. Admittedly, it's not what I would have done if the actual code is really this trivial.
It's not what you should have done in non-trivial code either.
There are cases where raising and catching your own exception is a good technique. For example, if you have nested loops, or if there are other exceptions that might be raised. It can make the code simpler and more readable.
1

UPDATED

I suppose like that

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

1 Comment

Assuming the original question was pseudocode and intended to check all fields against "N/A", this won't work. E.g. if XXX is "truthy", you'll throw the exception. (Same with YYY and ZZZ). These three aren't checked against "N/A".
1
for data in LL:
   try:
      if "N/A" in data:
         continue
      else:
         x, y, z = data
         # Process data...
   except Exception:
      continue

1 Comment

I like this, but would change the conditional to if 'N/A' in (x, y, z) - it's cleaner and easier to read.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.