0

I'm reading a csv file exactly like below:

HEADER1 HEADER2
First row1
Second row2

Now I need to check if it has any missing "headers" like HEADER1 or HEADER2 based on my inc_headers.

inc_headers = ['HEADER1','HEADER2','HEADER3']

def validatefields():
    inc_missing = []
    incident_reader = csv.DictReader(open(incident_path))
    for line in incident_reader:
        for field in inc_headers:
            if line[field]:
                pass
            else:
                inc_missing.append(field)
            continue
        break

In example above, I'm trying to see if HEADER3 is there and it should just append the field to the inc_missing list. Instead, the program breaks with an error. The program just completely breaks down and no longer follows the else or the except clause. I've also tried the try syntax below and it will still throw an error. How do I capture this error gracefully?

try:
    x = line[field]
except:
    ....
mkrieger1
  • 19,194
  • 5
  • 54
  • 65
Luigi
  • 439
  • 5
  • 23
  • It's not a "traceback error". "traceback" is additional information that is associated with the error. You should show it here. – mkrieger1 Jun 13 '21 at 23:23
  • @mkrieger1 it shows my inexperience in the new things I apologize. I'm coming from mf-rpgle – Luigi Jun 14 '21 at 18:34

1 Answers1

0

Codeblock below will fix the issue. Refer to this

sys.tracebacklimit = 0
try: 
    line[field]
except Exception as e:
    inc_missing.append(field)
Luigi
  • 439
  • 5
  • 23