Q:

how to skip enumerate encoding exception in python3?

I crafted script and preprocessed large csv for importing to database:
with open(sys.argv[1], encoding='utf-16') as _f:
    for i, line in enumerate(_f):
        try:
            .... some stuff with line ...
        except Exception as e:
            ...

But at some point it gives me exception on enumerate :
...
  File "/Users/elajah/PycharmProjects/untitled1/importer.py", line 94, in main
    for i, line in enumerate(_f):
  File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/codecs.py", line 319, in decode
    (result, consumed) = self._buffer_decode(data, self.errors, final)
  File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/encodings/utf_16.py", line 69, in _buffer_decode
    return self.decoder(input, self.errors, final)
UnicodeDecodeError: 'utf-16-le' codec can't decode byte 0x00 in position 0: truncated data
...

How to skip broken lines in file not interrupting the script flow ? 

A:

You can pass the parameter errors="ignore" to open, to tell Python that you don't care about encoding errors when reading from the file.
with open(sys.argv[1], errors="ignore") as _f:

This may behave oddly however, since it will just skip the invalid bytes, not the whole line the invalid bytes showed up on.
If the behavior you need is to ignore the whole line if anything goes wrong with the decoding, you might be better off reading the file in binary mode and trying the decoding yourself inside your try/except block, inside the loop:
with open(sys.argv[1], 'b') as _f:
    for i, line_bytes in enumerate(_f):
        try:
            line = line_bytes.decode('utf-16')
            # do some stuff with line ...
        except UnicodeDecodeError:
            pass

A final idea is to fix whatever is wrong with your file's data so you don't get decoding errors when reading it. But who knows how easy that is. If you're getting the file from somewhere else, out of your control, there may not be any practical way to fix it ahead of time.

