Question

My Python program has been getting a UnicodeDecodeError, so I thought I could use try-except in my code to bypass it. However, even with the try-except, I continue to get the UnicodeDecodeError and my program simply refuses to run. Am I using try-except wrong?

Here's my code:

combinedCorpus=[]
line = text.readline().lower()
words_filtered = [word for word in line.split() if len(word) >= 3]
try:
    combinedCorpus.append((words_filtered, "positive")) #this is where my problem is
except UnicodeDecodeError:
    print "Error appending to combinedCorpus."

Here is my traceback:

    Traceback (most recent call last):
  File "C:\Users\???\Desktop\python\App.py", line 38, in <module>
    print json.dumps(combinedCorpus,indent=2)
  File "C:\Python27\lib\json\__init__.py", line 250, in dumps
    sort_keys=sort_keys, **kw).encode(obj)
  File "C:\Python27\lib\json\encoder.py", line 209, in encode
    chunks = list(chunks)
  File "C:\Python27\lib\json\encoder.py", line 431, in _iterencode
    for chunk in _iterencode_list(o, _current_indent_level):
  File "C:\Python27\lib\json\encoder.py", line 332, in _iterencode_list
    for chunk in chunks:
  File "C:\Python27\lib\json\encoder.py", line 332, in _iterencode_list
    for chunk in chunks:
  File "C:\Python27\lib\json\encoder.py", line 313, in _iterencode_list
    yield buf + _encoder(value)
UnicodeDecodeError: 'utf8' codec can't decode bytes in position 4-5: invalid continuation byte
Was it helpful?

Solution

I found the solution to my problem. The unicode error actually came from later in the code.

combinedCorpus.append((words_filtered, "positive"))

print json.dumps(combinedCorpus,indent=2)

Apparently, json.dumps wasn't compatible with my text. Huh.

Thanks to everyone who answered and commented!

OTHER TIPS

Note that the Exception throws up level by level, I assume that the exception that the program throws out is not UnicodeDecodeError. If you try the top level exception, which should be Exception, it should be work.

Like below:

try:
    combinedCorpus.append((words_filtered, "positive"))
except Exception as e:
    print "Error appending to combinedCorpus."

If this works, you should try to find what the real exception is, and try to catch that one.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top