UnicodeDecodeError:'ascii'编解码器无法解码位置0中的字节0xc3:序数不在范围内(128)

use*_*653 5 python unicode google-app-engine decode

我正在使用google appengine python 2.5.

我在下面的代码中遇到unicodedecoderror,因为myuser名称具有以下值

userName     = unicode(userName).encode('utf-8') # äºï¼égãwmj is value in this variable  

userName     = unicode(userName).encode('utf-8')
strData = '{\"Sid\" :1, \"Oppid\" :%s, \"Aid\" :%s, \"EC\" :\"%s\", \"Name\" :\%s"' % (enemyID, userID, userEmpCode,userName)


   params = {'deviceToken'   : oDeviceToken,
              'message'       : strMessage,
              'CertificateId' : certificateId,
              'Data'          : strData
             }


result = urlfetch.fetch(url = url,
             payload = urllib.urlencode(params),
             method  = urlfetch.POST,
             headers = {"Authorization" : authString},
             deadline = 30
             )
Run Code Online (Sandbox Code Playgroud)

我在用户名上执行以下步骤将其编码为utf-8,以便我可以将其作为有效负载发送.

username = unicode(username).encode(utf-8)
Run Code Online (Sandbox Code Playgroud)

我相信当我打电话时会发生错误 urllib.urlencode(params)

请指导出错的地方......或者你可以..

什么应该是处理appengine python上的unicode字符串的最终策略..

我尝试了不同的解决方案阅读不同的线程..但仍然无法正常工作

oxc*_*oxc 7

你的问题似乎是,你打电话unicode(userName),不会对您已编码的字符串编码,所以它"默认为当前默认字符串编码",这似乎是ascii在你的情况.

你可能不应该unicode在任何情况下打电话,如果你知道它是一个unicode值,你已经很好了,如果没有,.decode用正确的编码调用.
如果你不确定,测试使用,isinstance因为尝试解码unicode值将导致另一个错误.