Python: how can I catch a exception and continue?
put the try
/except
inside the loop around the json.JSONEncoder().encode(item)
:
print "[",lst = [1, 2, 3, JsonedData(u'4'), 5]for i, item in enumerate(lst): try: chunk = json.JSONEncoder().encode(item) except TypeError: pass else: print chunk finally: # dont print the ',' if this is the last item in the lst if i + 1 != len(lst): print ","print "]"