Home >Backend Development >Python Tutorial >解决Python传递中文参数的问题

解决Python传递中文参数的问题

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOriginal
2016-06-10 15:08:162012browse

今天有个需要需要传递中文参数给URL

但是在GBK环境下的脚本传递GBK的参数老是给我报UNICODE的解码错误。烦的很。

所以我们果断选择用urlencode来处理中文,

由于国内外网站编码不同,国内是GBK的,国外是UTF8的。

>>> import sys
>>> sys.stdin.encoding
'GBK'
表示我们的环境是GBK的
>>> import urllib
>>> urllib.quote('编码坑爹')   
'%B1%E0%C2%EB%BF%D3%B5%F9'

这样我就得到了GBK的url编码,用这个编码直接传递给URL就能直接解释出中文。不需要我们再处理了

>>> urllib.quote('编码坑爹'.decode('gbk').encode('utf-8'))
'%E7%BC%96%E7%A0%81%E5%9D%91%E7%88%B9'

这是UTF-8的URL编码
非常方便,解决另外困扰一天的问题。。。

以上所述就是本文的全部内容了,希望大家能够喜欢。

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn