Nam*_*Ngo 1 python unicode google-app-engine non-ascii-characters
我需要发出一个POST请求,其中的数据可能是非ascii(中文,日文字符).我需要将输入转换为unicode并使用utf-8进行编码.我是这样做的:
foo = unicode(self.request.get('foo'), 'utf-8') #convert to unicode
foo = foo.encode('utf-8') #encode with utf-8
data = {'foo': foo}
payload = urllib.urlencode(data)
Run Code Online (Sandbox Code Playgroud)
但是,我在日志中不断收到此错误:
TypeError:不支持解码Unicode
Unicode无法解码,因为它已经是unicode.
试试这个:
if isinstance(var, str):
var = unicode(var, 'utf-8')
else:
var = unicode(var)
Run Code Online (Sandbox Code Playgroud)