【发布时间】:2020-09-07 08:37:53
【问题描述】:
str1="khloé kardashian"
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
UnicodeEncodeError: 'ascii' codec can't encode character u'\xe9' in position 4: ordinal not in range(128)
如何以完美的方式对其进行编码。 我正在尝试在烧瓶应用程序的 URL 中替换它:它在命令行上运行良好,但在应用程序中返回上述错误:
>>> url ="google.com/q=apple"
>>> url.replace("q=apple", "q={}".format(str1))
'google.com/q=khlo\xc3\xa9 kardashian'
【问题讨论】:
-
ASCII 没有字符“é”。你到底想完成什么?
-
另外,这是 Python 2.7 还是 3.x?请指定,因为它们处理 Unicode 字符的方式不同。
-
@Anwarvic 这是因为浏览器会为您处理编码细节。例如参见tools.ietf.org/html/rfc3986 的第 2 部分
标签: python python-2.7 unicode python-unicode unicode-string