2 Answers 2
To convert u'杭州' to '%BA%BC%D6%DD':
In [24]: ''.join('%{0:X}'.format(ord(c)) for c in u"杭州".encode('gbk'))
Out[24]: '%BA%BC%D6%DD'
In [25]: ''.join('%{0:X}'.format(ord(c)) for c in u"大连".encode('gbk'))
Out[25]: '%B4%F3%C1%AC'
To convert '%BA%BC%D6%DD' to u'杭州':
In [54]: import binascii
In [55]: print(binascii.unhexlify(''.join('%BA%BC%D6%DD'.split('%'))).decode('gbk'))
杭州
answered Jun 26, 2011 at 1:23
unutbu
887k197 gold badges1.9k silver badges1.7k bronze badges
Sign up to request clarification or add additional context in comments.
2 Comments
jack
thanks for your answer. it works. could you please teach me how to convert strings like '%BA%BC%D6%DD' back to u"杭州"?
unutbu
@jack: I've added a way to convert
'%BA%BC%D6%DD' back to u"杭州". Note however that Dietrich Epp provided a better answer (using urllib.parse.quote and/or urllib.parse.unquote) if your Python version is modern enough (e.g. Python3).You'll need to give us more information.
def encode(s):
if s == "杭州":
return "%BA%BC%D6%DD"
if s == "大连":
return "%B4%F3%C1%AC"
raise ValueError
You need to tell us the encoding you're using, for one thing.
import urllib.parse
def encode(s):
return urllib.parse.quote(s.encode('gbk'))
answered Jun 26, 2011 at 1:24
Dietrich Epp
216k39 gold badges366 silver badges427 bronze badges
Comments
Explore related questions
See similar questions with these tags.
lang-py