[Python] UnicodeDecodeError: ‘ascii’ codec can’t decode byte 0xe3
Python + Google App Engine の勉強を下記サイトに沿ってしていたところ、文字コードでつまずいて解決したお話です。
【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() |
post で渡された日本語文字を表示させようとしたら下記のようなエラーが表示されました。
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/bakorer_mac/Dropbox/Program/GAE/bakorer-labo/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)
文字コードでつまずいたらここを読んで復習するようにしています。
・参考:PythonのUnicodeEncodeErrorを知る – HDEラボ
17行目を以下のように修正すればエラーが消えました。
text1 = self.request.get('text1').encode('utf-8') |
以上です。