Python Unicode UnicodeEncodeError

Joh*_*ger 1 python unicode google-app-engine utf-8

我在尝试将UTF-8字符串转换为unicode时遇到问题.我收到了错误.

UnicodeEncodeError: 'ascii' codec can't encode characters in position 73-75: ordinal not in range(128)
Run Code Online (Sandbox Code Playgroud)

我尝试在try/ except块中包装它,但谷歌给了我一个系统管理员错误,这是一行.有人可以建议如何捕获此错误并继续.

干杯,约翰.

- 完全错误 -

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 501, in __call__
    handler.get(*groups)
  File "/Users/johnb/Sites/hurl/hurl.py", line 153, in get
    self.redirect(url.long_url)
  File "/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/google/appengine/ext/webapp/__init__.py", line 371, in redirect
    self.response.headers['Location'] = str(absolute_url)
UnicodeEncodeError: 'ascii' codec can't encode characters in position 73-75: ordinal not in range(128)
Run Code Online (Sandbox Code Playgroud)

Ale*_*lli 8

正确的解决方案是执行以下操作:

self.response.headers['Location'] = urllib.quote(absolute_url.encode("utf-8"))
Run Code Online (Sandbox Code Playgroud)