Catch error in a for loop python

后端 未结 3 515
既然无缘
既然无缘 2021-01-20 11:00

I have a for loop on an avro data reader object

for i in reader:
    print i

then I got a unicode decode error in the for statement so I wa

3条回答
  •  萌比男神i
    2021-01-20 11:32

    You need the try/except inside the loop:

        for i in reader:
           try: 
               print i
           except UnicodeEncodeError:
               pass
    

    By the way it's good practice to specify the specific type of error you're trying to catch (like I did with except UnicodeEncodeError:, since otherwise you risk making your code very hard to debug!

提交回复
热议问题