简体   繁体   English

Python Unicode UnicodeEncodeError

[英]Python Unicode UnicodeEncodeError

I am having issues with trying to convert an UTF-8 string to unicode. 我在尝试将UTF-8字符串转换为unicode时遇到问题。 I get the error. 我得到了错误。

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

I tried wrapping this in a try / except block but then google was giving me a system administrator error which was one line. 我尝试将其包装在try / except块中,但随后Google给我一个系统管理员错误,即一行。 Can someone suggest how to catch this error and continue. 有人可以建议如何捕获此错误并继续。

Cheers, John. 干杯,约翰。

-- FULL ERROR -- -全错误-

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)

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

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

The location header you are trying to set needs to be an Url, and an Url needs to be in Ascii. 您尝试设置的位置标头必须为Url,而Url必须位于Ascii中。 Since your Url is not an Ascii string you get the error. 由于您的网址不是Ascii字符串,因此会出现错误。 Just catching the error won't help since the Location header won't work with an invalid Url. 仅捕获错误将无济于事,因为Location标头不适用于无效的网址。

When you create absolute_url you need to make sure it is encoded properly, best by using urllib.quote and the strings encode() method. 当您创建absolute_url您需要确保其编码正确,最好使用urllib.quote和字符串encode()方法。 You can try this: 您可以尝试以下方法:

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

Please edit that mess so that it's legible. 请编辑该混乱内容,以使其清晰可见。 Hint: use the "code block" (101010 thingy button). 提示:使用“代码块”(101010常用按钮)。

You say that you are "trying to convert an UTF-8 string to unicode" but str(absolute_url) is a strange way of going about it. 您说您正在“尝试将UTF-8字符串转换为unicode”,但是str(absolute_url)是解决该问题的一种奇怪方法。 Are you sure that absolute_url is UTF-8? 您确定absolute_url为UTF-8吗? Try 尝试

print type(absolute_url)
print repr(absolute_url)

If it is UTF-8, you need absolute_url.decode('utf8') 如果 UTF-8,则需要absolute_url.decode('utf8')

Try this: 尝试这个:

self.response.headers['Location'] = absolute_url.decode("utf-8")
or
self.response.headers['Location'] = unicode(absolute_url, "utf-8")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM