python - 用python进行简单的ascii url编码

标签 python urlencode turbogears turbogears2

看看那个:

import urllib
print urllib.urlencode(dict(bla='Ã'))

输出是

bla=%C3%BC

我想要的很简单,我想要 ascii 而不是 utf-8 的输出,所以我需要输出:

bla=%C3

如果我尝试:

urllib.urlencode(dict(bla='Ã'.decode('iso-8859-1')))

不起作用(我所有的 python 文件都是 utf-8 编码的):

'ascii' 编解码器无法对位置 0-1 中的字符进行编码:序号不在范围内 (128)

在生产中,输入是统一编码的。

最佳答案

看看unicode transliteration in python :

from unidecode import unidecode
print unidecode(u"\u5317\u4EB0")

# That prints: Bei Jing

在你的情况下:

bla='Ã'
print unidecode(bla)
'A'

这是第三方库,可以通过以下方式轻松安装:

$ git clone http://code.zemanta.com/tsolc/git/unidecode
$ cd unidecode
$ python setup.py install

关于python - 用python进行简单的ascii url编码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3114176/

相关文章:

用于创建可点击链接的Python代码抛出错误

python - 如何使用mysql在数据库端声明默认高精度日期时间default + onupdate

Javascript + python url-en/解码问题

python - Turbogears 2 : authentication, 密码在不同表中,更新时反馈

python - 如何在 TurboGears 2.2.2 的 RootController 中使用自定义 sys.excepthook

python - 读取 CSV 文件并在 python 中的每个项目上加上双引号

functional-programming - 如何在对象的多个方法上使用 functools.partial 并无序卡住参数?

php - 字符串中的与号导致长度/比较不正确

python - 如何请求已在 python-requests 中进行 URL 编码的 URL?

python - 适合 Python/TurboGears 网络开发人员的面试问题?