bobsmith76
bobsmith76

Reputation: 292

Processing a for loop with an error

I have a for loop which has an error in it.

try:
    for line in text:
        'do stuff'
except:
    pass

When the error occurs python just exits the for loop. I can't get python to ignore the error and keep iterating through the loop. Incidentally, it is a text file and I am looping through the lines. I should also point out that the error does not occur in the do stuff part, it literally occurs in the for loop. I also frankly don't understand why the error is being thrown since the line is just like any other line. I tried deleting the line to see if it was just a one time thing but the next line has an error in it too which leads me to believe that I cannot just delete bad lines. The name of the error is unicodedecodeerror

Here's the text:

https://drive.google.com/file/d/0B9zzW6-3m2qGVFRTbzlXMS0tVUU/view?usp=sharing

I'm trying to make a list of all the words that follow the word 'abstract'

Here's the actual code

with open(full_path_of_old_file) as old:

for i, line in enumerate(old):


    if "abstract," not in line:
        b = line.find('abstract')
        line2 = line[b+9:]
        list1 = line2.split()
        list1[0] = list1[0].replace(",","")
        list1[0] = list1[0].replace(".", "")
        try:
            if list1[0][-1] == "s":
                list1[0] = list1[0][:-1]
        except:
            pass
        objects_of_abstract.append(list1[0])

Here's the full traceback

Traceback (most recent call last):
  File "/Applications/PyCharm CE.app/Contents/helpers/pydev/pydevd.py", line 1596, in <module>
    globals = debugger.run(setup['file'], None, None, is_module)
  File "/Applications/PyCharm CE.app/Contents/helpers/pydev/pydevd.py", line 1023, in run
    pydev_imports.execfile(file, globals, locals)  # execute the script
  File "/Applications/PyCharm CE.app/Contents/helpers/pydev/_pydev_imps/_pydev_execfile.py", line 18, in execfile
    exec(compile(contents+"\n", file, 'exec'), glob, loc)
  File "/Users/kylefoley/PycharmProjects/inference_engine2/inference2/Proofs/z_natural_language.py", line 25, in <module>
    for i, line in enumerate(old):
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/codecs.py", line 321, in decode
    (result, consumed) = self._buffer_decode(data, self.errors, final)
UnicodeDecodeError: 'utf-8' codec can't decode byte 0xe9 in position 764: invalid continuation byte
We've got an error while stopping in post-mortem: <class 'KeyboardInterrupt'>

Upvotes: 0

Views: 139

Answers (5)

jeremycg
jeremycg

Reputation: 24945

A possible answer, which should bypass your problem to the other answers, is to use the suppress function from the contextlib module in the standard library:

from contextlib import suppress

with suppress(UnicodeDecodeError): #might have to some some work here to get the error right
    for i, line in enumerate(old):
        .....

as it says in the docs though, better to try and fix your problem, rather than silently ignore it, if possible.

Upvotes: 1

Paul Cornelius
Paul Cornelius

Reputation: 10936

Somewhere in your code you open the file. There are two parameters in the open function that pertain to your problem:

encoding=None: encoding is the name of the encoding used to decode or encode the file. This should only be used in text mode. The default encoding is platform dependent (whatever locale.getpreferredencoding() returns), but any encoding supported by Python can be used. See the codecs module for the list of supported encodings.

errors=None: errors is an optional string that specifies how encoding and decoding errors are to be handled–this cannot be used in binary mode. A variety of standard error handlers are available, though any error handling name that has been registered with codecs.register_error() is also valid.

[there is more; see the standard library docs]

Since you don't show how you open the file, I can't tell you specifically what is wrong. But the error message indicates that it is an encoding error, and you should start by looking into that.

Upvotes: 1

Cory Madden
Cory Madden

Reputation: 5193

Your problem is your file encoding. Change it to latin-1 like this:

objects_of_abstract = list()
with open('abstract.txt', encoding="latin-1") as old:

    for i, line in enumerate(old):


        if "abstract," not in line:
            b = line.find('abstract')
            line2 = line[b+9:]
            list1 = line2.split()
            list1[0] = list1[0].replace(",","")
            list1[0] = list1[0].replace(".", "")
            try:
                if list1[0][-1] == "s":
                    list1[0] = list1[0][:-1]
            except:
                pass
            objects_of_abstract.append(list1[0])

Upvotes: 4

Jishnunand P K
Jishnunand P K

Reputation: 290

You have to keep try/except block inside for loop

for line in text:
    try:
        # your Operation
    except:
        pass

Upvotes: 0

ContinuousLoad
ContinuousLoad

Reputation: 4912

This should help with the first question (keeping the loop going):

In a python try...except block, it will try to run all the code in the try block, and if an error is thrown, it will stop and move to the except

In your case, you could move the for outside the try, so that if an error occurs it will be handled in the except and then continue on to the next iteration:

for line in text:
  try:
    'do stuff'
  except: # if 'do stuff' throws an error, we just go to the next iteration
    pass

Upvotes: 0

Related Questions