Forums

UnicodeEncodeError

Hi, Can anyone help me in resolving the following issue? I am able to run the same code in my local machine using python2.7 in windows7 and fedora23 without any error. But in pythonanywhere, I am getting the following error.

Thanks in advance, RM

[2017-12-26 21:56:31,047] ERROR in app: Exception on /get [GET]
2017-12-26 21:56:31,050: Traceback (most recent call last):
2017-12-26 21:56:31,050:   File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1988, in wsgi_app
2017-12-26 21:56:31,050:     response = self.full_dispatch_request()
2017-12-26 21:56:31,051:   File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1641, in full_dispatch_request
2017-12-26 21:56:31,051:     rv = self.handle_user_exception(e)
2017-12-26 21:56:31,051:   File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1544, in handle_user_exception
2017-12-26 21:56:31,051:     reraise(exc_type, exc_value, tb)
2017-12-26 21:56:31,051:   File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1639, in full_dispatch_request
2017-12-26 21:56:31,051:     rv = self.dispatch_request()
2017-12-26 21:56:31,051:   File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1625, in dispatch_request
2017-12-26 21:56:31,051:     return self.view_functions[rule.endpoint](**req.view_args)
2017-12-26 21:56:31,051:   File "/home/penguine/mysite/flask_app.py", line 22, in get_bot_response
2017-12-26 21:56:31,051:     return str(english_bot.get_response(userText))
2017-12-26 21:56:31,052: UnicodeEncodeError: 'ascii' codec can't encode characters in position 0-7: ordinal not in range(128)
2017-12-26 21:56:31,047: Exception on /get [GET]#012Traceback (most recent call last):#012  File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1988, in wsgi_app#012    response = self.full_dispatch_request()#012  File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1641, in full_dispatch_request#012    rv = self.handle_user_exception(e)#012  File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1544, in handle_user_exception#012    reraise(exc_type, exc_value, tb)#012  File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1639, in full_dispatch_request#012    rv = self.dispatch_request()#012  File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1625, in dispatch_request#012    return self.view_functions[rule.endpoint](**req.view_args)#012  File "/home/penguine/mysite/flask_app.py", line 22, in get_bot_response#012    return str(english_bot.get_response(userText))#012UnicodeEncodeError: 'ascii' codec can't encode characters in position 0-7: ordinal not in range(128)

Hi there,

PythonAnywhere may not have the same default encoding as your machine. The solution is to be explicit about your text encode and decode operations. So, for example, if

english_bot.get_response(userText)

is returning raw bytes, try explicitly converting it to a string using the utf8 encoding with:

english_bot.get_response(userText).decode('utf8')

more info on unicode and character encoding in this excellent article by Joel Spolsky