Skip to main content
 首页 » 编程设计

Python 统一码 UnicodeEncodeError

2025年05月04日12xiaohuochai

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

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

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

干杯,约翰。

-- 完全错误 --

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) 

请您参考如下方法:

正确的solution就是做以下事情:

self.response.headers['Location'] = urllib.quote(absolute_url.encode("utf-8"))