使用Python进行简单的ascii url编码 [英] Simple ascii url encoding with python

查看:302
本文介绍了使用Python进行简单的ascii url编码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

看看:

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

输出为

bla=%C3%BC

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

what I want is simple, I want the output in ascii instead of utf-8, so I need the output:

bla=%C3

如果我尝试:

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

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

doesn't work (all my python files are utf-8 encoded):

'ascii' codec can't encode characters in position 0-1: ordinal not in range(128)

在生产中,输入未编码.

In production, the input comes unicoded.

推荐答案

感谢所有解决方案.你们所有人都汇合到了同一点. 我一团糟,更改了正确的代码

thanks to all solutions. all of you converge to the very same point. I made a mess changing the right code

.encode('iso-8859-1') 

.decode('iso-8859-1')

返回到.encode('iso-8859-1')即可.

turn back to .encode('iso-8859-1') and it works.

这篇关于使用Python进行简单的ascii url编码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆