1

I'm working on Flask (which has probably nothing to do with this problem) web app which uses python. I'm getting this error: UnicodeDecodeError: 'utf8' codec can't decode byte 0xe8 in position 5: invalid continuation byte . I put # -*- coding: utf-8 -*- on the top of the file but still getting the same. This is a part of the code:

s+="Searching results for: \"%s \"<br><br>".encode("utf-8") % (pattern)

    for i in orderedList[0:10]:
        a=a+1
        res=(SC.dictionaryMemory.get(i))

        try:
            resString= ', '.join(res)
        except:
            print("Chyba!")

        with open('somefile.txt', 'a') as the_file:
            the_file.write(resString)

        s+="<b>%s: </b>%s,<br>".decode("utf-8") % (i, resString.decode("utf-8"))

    return (s.decode("utf-8") if (len(orderedList)>0) else "Nebyli nalezeny zadne vyrazy.".encode("utf-8")))

And this is s traceback I get on html page:

File "C:\Python27\lib\site-packages\flask\app.py", line 1836, in __call__
return self.wsgi_app(environ, start_response)
File "C:\Python27\lib\site-packages\flask\app.py", line 1820, in wsgi_app
response = self.make_response(self.handle_exception(e))
File "C:\Python27\lib\site-packages\flask\app.py", line 1403, in handle_exception
reraise(exc_type, exc_value, tb)
File "C:\Python27\lib\site-packages\flask\app.py", line 1817, in wsgi_app
response = self.full_dispatch_request()
File "C:\Python27\lib\site-packages\flask\app.py", line 1477, in full_dispatch_request
rv = self.handle_user_exception(e)
File "C:\Python27\lib\site-packages\flask\app.py", line 1381, in handle_user_exception
reraise(exc_type, exc_value, tb)
File "C:\Python27\lib\site-packages\flask\app.py", line 1475, in full_dispatch_request
rv = self.dispatch_request()
File "C:\Python27\lib\site-packages\flask\app.py", line 1461, in dispatch_request
return self.view_functions[rule.endpoint](**req.view_args)
File "D:\workspace\AproximateSearchPython2\WebApp.py", line 74, in search
s+="<b>%s: </b>%s,<br>".decode("utf-8") % (i, resString.decode("utf-8"))
File "C:\Python27\lib\encodings\utf_8.py", line 16, in decode
return codecs.utf_8_decode(input, errors, True)
UnicodeDecodeError: 'utf8' codec can't decode byte 0xe8 in position 5: invalid continuation byte

The problem is in my opinion in content of resString. In somefile.txt is this:

společné, obecný, veřejný, společný, prostý, obyčejný, obvyklý, obecný, společný, běľný

Will you give me some advice please?

EDIT: I changed decode to encode, but the same problem appeared.

File "C:\Python27\lib\site-packages\flask\app.py", line 1836, in __call__
return self.wsgi_app(environ, start_response)
File "C:\Python27\lib\site-packages\flask\app.py", line 1820, in wsgi_app
response = self.make_response(self.handle_exception(e))
File "C:\Python27\lib\site-packages\flask\app.py", line 1403, in handle_exception
reraise(exc_type, exc_value, tb)
File "C:\Python27\lib\site-packages\flask\app.py", line 1817, in wsgi_app
response = self.full_dispatch_request()
File "C:\Python27\lib\site-packages\flask\app.py", line 1477, in full_dispatch_request
rv = self.handle_user_exception(e)
File "C:\Python27\lib\site-packages\flask\app.py", line 1381, in handle_user_exception
reraise(exc_type, exc_value, tb)
File "C:\Python27\lib\site-packages\flask\app.py", line 1475, in full_dispatch_request
rv = self.dispatch_request()
File "C:\Python27\lib\site-packages\flask\app.py", line 1461, in dispatch_request
return self.view_functions[rule.endpoint](**req.view_args)
File "D:\workspace\AproximateSearchPython2\WebApp.py", line 75, in search
s+="<b>%s: </b>%s,<br>".encode("utf-8") % (i, resString.encode("utf-8"))
UnicodeDecodeError: 'utf8' codec can't decode byte 0xe8 in position 5: invalid continuation byte
Milano
  • 18,048
  • 37
  • 153
  • 353
  • 1
    _decode_ from bytes. _encode_ from unicode. – wim Jun 02 '14 at 17:38
  • I've tried to change decode to decode... but it is the same... It's weird... I've put the traceback up there as edit.7 – Milano Jun 02 '14 at 17:46
  • 1
    Don't play encode / decode whack-a-mole. Get your data straight, know when you have bytes and when you have unicode, no guesswork! – wim Jun 02 '14 at 17:55
  • This might help: http://stackoverflow.com/a/9644115/674039 – wim Jun 02 '14 at 17:59

1 Answers1

0
s+= unicode("""Searching results for: "{pattern}" <br><br>""").encode("utf-8").format(pattern=pattern)
Ricky Wilson
  • 3,187
  • 4
  • 24
  • 29