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:
....