我需要发出一个 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)
但是,我的日志中不断出现此错误:
TypeError: decoding Unicode is not supported
请您参考如下方法:
Unicode无法解码,因为它已经是unicode了。
试试这个:
if isinstance(var, str):
var = unicode(var, 'utf-8')
else:
var = unicode(var)
