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)
如何以完美的方式对其进行编码。 我正在尝试在flask应用程序的URL中替换此内容:它在命令行上运行良好,但在应用程序中返回上述错误:
>>> url ="google.com/q=apple"
>>> url.replace("q=apple", "q={}".format(str1))
'google.com/q=khlo\xc3\xa9 kardashian'
根据标准,URL中不能有
é
。您需要使用the appropriate URL encoding,内置的urllib
包使用is handled您应该使用
urllib
来正确构造URL。您的URL中还有其他问题,例如,空白urllib
照顾他们改用utf-8
相关问题 更多 >
编程相关推荐