[Python] UnicodeDecodeError: 'ascii' codec can't decode byte 0xe3
This is a story about how I stumbled on character encoding while studying Python + Google App Engine following the site below and resolved it.
 
【libro】 PythonによるGoogle App Engine(GAE)プログラミング入門
#!/usr/bin/env python
#-*- coding: utf-8 -*-
#
import os
from google.appengine.ext import webapp, db
from google.appengine.ext.webapp import util, template
class MainHandler(webapp.RequestHandler):
  def get(self):
    params = {'message':'これは、テンプレートで出力したWebページです。'}
    fpath = os.path.join(os.path.dirname(__file__),'views','index.html')
    html = template.render(fpath, params)
    self.response.headers['Content-Type'] = 'text/html'
    self.response.out.write(html)
  
  def post(self):
    text1 = self.request.get('text1')
    params = {'message':'こんにちは、' + text1 + 'さん!'}
    fpath = os.path.join(os.path.dirname(__file__),'views','index.html')
    html = template.render(fpath, params)
    self.response.headers['Content-Type'] = 'text/html'
    self.response.out.write(html)
def main():
  application = webapp.WSGIApplication([('/', MainHandler)],
                                         debug=True)
  util.run_wsgi_app(application)
if __name__ == '__main__':
  main()
When I tried to display Japanese characters passed through post, the following error appeared.
Traceback (most recent call last): File "/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/google/appengine/ext/webapp/__init__.py", line 636, in __call__ handler.post(*groups) File "/Users/your_username/gae/sandbox/main.py", line 20, in post params = {'message':'こんにちは、' + text1 + 'さん!'} UnicodeDecodeError: 'ascii' codec can't decode byte 0xe3 in position 0: ordinal not in range(128)
When I encounter character encoding issues, I always refer to this resource for review.
・Reference: PythonのUnicodeEncodeErrorを知る - HDEラボ
The error was resolved by modifying line 17 as follows.
    text1 = self.request.get('text1').encode('utf-8')
That’s all from the Gemba.