python - How to handle exceptions when reading rows of a file? -
is there way handle exceptions when reading specific row of file?
for example, have block:
with open(filein, 'rb') f:     reader = csv.reader(f, delimiter='\t')     i, row in enumerate(reader):         try:             # stuff         except:             pass   and, after parsing half file, error
ioerror: [errno 22] invalid argument
on line
     i, row in enumerate(reader):   and i'd continue parsing file, skipping problem row.
for...reader repeatedly calls next(reader).  intercept exception when looping, looping , make next calls yourself.  untested:
with open(filein, 'rb') f:     reader = csv.reader(f, delimiter='\t')     = -1     while true:         += 1         try:             row = next(reader)         except stopiteration:             break         except ioerror:             pass         else:             try:                 # stuff             except:                 pass      
Comments
Post a Comment