UTF-8百分比编码和python [英] UTF-8 percentage encoding and python
本文介绍了UTF-8百分比编码和python的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试让python给我百分比编码的字符串.我正在与之交互的API(我认为它使用的是百分比编码的UTF-8),为î提供了%c3%ae.但是,python的urllib.quote给出了%3F.
I'm trying to get python to give me percent encoded strings. The API I'm interacting with (which I think is using percent encoded UTF-8), gives %c3%ae for î. However, python's urllib.quote gives %3F.
import urllib
mystring = "î"
print urllib.quote(mystring)
print urllib.quote_plus(mystring)
print urllib.quote(mystring.encode('utf-8'))
任何帮助表示赞赏.
推荐答案
您的文件在引用字符串之前必须将其编码为utf-8
,并且该字符串应为unicode.另外,您还必须在coding
部分中为源文件指定适当的文件编码:
Your file has to encode your string as utf-8
before quoting it, and the string should be unicode. Also you have to specify the appropriate file encoding for your source file in the coding
section:
# -*- coding: utf-8 -*-
import urllib
s = u'î'
print urllib.quote(s.encode('utf-8'))
给我输出:
%C3%AE
这篇关于UTF-8百分比编码和python的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文