在Python中发送非ASCII POST请求?

Bo *_*ich 7 html python unicode post http

我正在尝试向网络应用发送POST请求.我正在使用mechanize模块(本身是urllib2的包装器).无论如何,当我尝试发送POST请求时,我得到了UnicodeDecodeError: 'ascii' codec can't decode byte 0xc5 in position 0: ordinal not in range(128).我试图把unicode(string)unicode(string, encoding="utf-8"),unicode(string).encode()等等,没有什么工作-无论是返回的错误之上,或TypeError: decoding Unicode is not supported

我查看了类似问题的其他SO答案,但都没有帮助.

提前致谢!

编辑:产生错误的示例:

prda = "š???" #valid UTF-8 characters
prda # typing in python shell 
'\xc5\xa1\xc4\x91\xc4\x87\xc4\x8d'
print prda # in shell
š???
prda.encode("utf-8") #in shell
UnicodeDecodeError: 'ascii' codec can't decode byte 0xc5 in position 0: ordinal not in range(128)
unicode(prda)
UnicodeDecodeError: 'ascii' codec can't decode byte 0xc5 in position 0: ordinal not in range(128)
Run Code Online (Sandbox Code Playgroud)

Lau*_*ves 8

我假设您使用的是Python 2.x.

给定一个unicode对象:

myUnicode = u'\u4f60\u597d'
Run Code Online (Sandbox Code Playgroud)

使用utf-8编码:

mystr = myUnicode.encode('utf-8')
Run Code Online (Sandbox Code Playgroud)

请注意,您需要明确指定编码.默认情况下,它(通常)使用ascii.